过去,检查鼠标是否存在的最佳方法是查找触摸事件支持。然而,桌面版Chrome现在支持触摸事件,这使得这个测试失误了。
有没有一种直接测试mouseover事件支持的方法,而不是根据触摸事件的存在来推断呢?
解决方案:以下是基于AshleysBrain答案的代码,它可以正常工作。
有没有一种直接测试mouseover事件支持的方法,而不是根据触摸事件的存在来推断呢?
解决方案:以下是基于AshleysBrain答案的代码,它可以正常工作。
jQuery(function()
{
// Has mouse
jQuery("body").one("mousemove", function(e)
{
attachMouseEvents();
});
// Has touchscreen
jQuery("body").one("touchstart", function(e)
{
// Unbind the mouse detector, as this will fire on some touch devices. Touchstart should always fire first.
jQuery("body").unbind("mousemove");
attachTouchEvents();
});
});