2017-03-17 133 views
0

我点击img div时添加了警报。我收到多个单击的警报。在网络界面上,有时我会收到单一警报,但在移动设备上它会执行多次。轻击事件不适用于第一次轻敲。它正在为第二次点击工作。JQuery弹出警告三次,但一次执行

<html> 
<body> 
plug:<img id="plug" src="http://icons.iconarchive.com/icons/zeusbox/gartoon/32/socket-icon.png"> 
</body> 
</html> 
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script> 
<script src="https://code.jquery.com/mobile/1.5.0-alpha.1/jquery.mobile-1.5.0-alpha.1.min.js"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script> 

<script> 
$(document).ready(function(){ 
    $("#plug").on('tap', function() { 
     alert("plese long press"); 
    }); 
}); 
</script> 
+1

为什么您的脚本不在'html'标记之外? – Laazo

+0

是这样的问题吗? – BhanuPrakash

+0

并非完全,但有些浏览器可能会忽略你的脚本,因为这是一个格式不正确的文件 – Laazo

回答

0

您在控制台中收到此错误。

Uncaught TypeError: Cannot read property 'add' of undefined 

i think your JQuery version and JQuery-ui version not compatible. 

change your jquery-ui with this one 

    <script 
     src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" 
     integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" 
     crossorigin="anonymous"></script> 

that should solve the problem 
0

在你的事件添加一个preventDefault()停止事件触发不止一次:

$("#plug").unbind('tap').on('tap', function(event) { 
    event.preventDefault(); 
    alert("please long press"); 
}); 
+0

但它再次调用三次,并且轻击事件正在为移动版本中的第二次敲击工作 – BhanuPrakash

+0

好吧,添加一个解除绑定的调用进入轻敲事件的绑定。 –