jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery mobile tap event触发两次大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我用 jquery mobile’tap’事件测试,发现它被触发两次,每次触发. html页面代码如下:
<!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.

<!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 mobile tap event触发两次全部内容,希望文章能够帮你解决jQuery mobile tap event触发两次所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。