2017-03-17 139 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

好吧,添加一個解除綁定的調用進入輕敲事件的綁定。 –