【发布时间】:2012-02-24 09:07:19
【问题描述】:
过去,检查鼠标是否存在的最佳方法是查找touch event support。但是,桌面 Chrome 现在支持触摸事件,导致此测试失败。
有没有办法直接测试鼠标悬停事件支持,而不是根据触摸事件的存在来推断它?
解决方案:根据 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();
});
});
【问题讨论】:
-
太棒了!尝试使用剧本(RIM OS),第一次工作正常,但在页面刷新时它检测到鼠标移动(!?)。但是,将测试附加到某个 #id 而不是 body 效果很好。
标签: javascript mobile browser browser-feature-detection