jQuery mobile tap event触发两次

前端之家收集整理的这篇文章主要介绍了jQuery mobile tap event触发两次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用 jquery mobile’tap’事件测试,发现它被触发两次,每次触发. html页面代码如下: @H_301_2@<!DOCTYPE html> <html> <head> <title>Demo</title> <Meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.js"></script> <style> #Box{ background-color:red; width:200px; height:200px; } </style> </head> <body> <div id="Box"> tapped me </div> <script type="text/javascript"> $(document).ready(function() { $('#Box').bind('tap',function(event) { alert('why'); }); }); </script> </body> </html>

更讽刺的是,当我通过jsfiddle测试这个,它只是发射了一次事件.

这是链接.
http://jsfiddle.net/mochatony/tzQ6D/6/

经过进一步的测试,我发现bug已经消失了放置在标题部分的JavaScript.

@H_301_2@<!DOCTYPE html> <html> <head> <title>Demo</title> <Meta name="viewport" content="width=device-width,user-scalable=no"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script> <script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.js"> </script> <style type="text/css"> #Box{ background-color:red; width:200px; height:200px; } </style> <script type="text/javascript"> $(document).ready(function() { $('#Box').bind('tap',function(event) { alert('halo'); }); }); </script> </head> <body> <div id="Box"> tapped me </div> </body> </html>

我无法解释为什么身体和头部部分的位置使得这不同.

解决方法

我忘了我在哪里看到这个,但是问题是jQuery Mobile绑定到触摸和鼠标事件,以模拟“点击”,在某些情况下,Android会同时触发.

为我工作的窍门是在事件处理程序中的事件上调用preventDefault.这将使重复事件不会触发.

至于为什么这种情况只是有时,我知道如果有人没有听触摸版本,移动浏览器将尝试只触发该事件的鼠标版本.检测可能存在一个问题,或者是jQuery的代理,这个启发式是混乱的.

猜你在找的jQuery相关文章