Ale*_*per 7 mobile jquery jquery-mobile cordova
我有一个简单的JQuery移动应用程序的问题.这一切都归结为点击事件两次触发.
没有类似问题的解决方案解决了这个问题.
无论是部署到设备还是在浏览器中显示,都会出现问题.
这是代码
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script src="jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="jquery.mobile-1.2.0.min.js" type="text/javascript"></script>
<script src="cordova-2.2.0.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).bind("mobileinit", function () {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
</script>
<link rel="Stylesheet" href="jquery.mobile-1.2.0.min.css" />
Run Code Online (Sandbox Code Playgroud)
<div>
<input type="button" id="ButtonSubmit" value="Save" />
</div>
Run Code Online (Sandbox Code Playgroud)
<script type="text/javascript">
$("#ButtonSubmit").click(function () {
alert('Clicked');
});
</script>
Run Code Online (Sandbox Code Playgroud)
您可以通过使用下面提到的方式来解决这个问题。
首先使用解除绑定,然后像下面这样绑定:
<script type="text/javascript">
$("#ButtonSubmit").unbind('click').bind('click', function () {
alert('Clicked');
return false; //to prevent the browser actually following the links!
});
</script>
Run Code Online (Sandbox Code Playgroud)
更新:如果您有关于方法,那么你可以使用如下所示。
注意:当您使用off方法时,它会删除以前的点击事件处理程序,而使用on方法会添加新的事件处理程序。因为它不允许发生 2 次点击。
<script type="text/javascript">
$('#ButtonSubmit').off('click').on('click', function(){
alert('Clicked');
return false; //to prevent the browser actually following the links!
});
</script>
Run Code Online (Sandbox Code Playgroud)