jquery移动点击事件触发两次
我有一个简单的JQuery移动应用程序的问题。 这一切都归结为点击事件两次触发。
没有类似问题的解决方案解决了这个问题。
无论是部署到设备还是在浏览器中显示,都会出现问题。
这是代码
$(document).bind("mobileinit", function () { $.support.cors = true; $.mobile.allowCrossDomainPages = true; });
$("#ButtonSubmit").click(function () { alert('Clicked'); });
您可以使用下面提到的方法解决这个问题 。
首先使用unbind然后绑定如下:
更新:如果你有方法,那么你可以使用如下。
注意:当您使用off方法时,它会删除以前的单击事件处理程序,并且使用on方法会添加新的one.B’cos,它不允许发生单击2次。
在最新的jQuery移动设备中发生了许多实验性的事情,触发点击事件,这可能导致这些问题(第2689行):
// This plugin is an experiment for abstracting away the touch and mouse // events so that developers don't have to worry about which method of input // the device their document is loaded on supports. // // The idea here is to allow the developer to register listeners for the // basic mouse events, such as mousedown, mousemove, mouseup, and click, // and the plugin will take care of registering the correct listeners // behind the scenes to invoke the listener at the fastest possible time // for that device, while still retaining the order of event firing in // the traditional mouse environment, should multiple handlers be registered // on the same element for different events. // // The current version exposes the following virtual events to jQuery bind methods: // "vmouseover vmousedown vmousemove vmouseup vclick vmouseout vmousecancel"
http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.js
我修复它的方式是:
$('#mobileMenuItem').off('click');
然后它开始正常运作。
我认为这与你如何处理你的活动有关。 即,使用pageinit。
此外,可能隐藏了具有不同data-url的页面,因此无论如何,相同的js也会运行。
在您的代码中,我会做以下更改
以这种方式,我知道我将绑定一次。
您是否尝试阻止默认操作?
如果您的onclick处理程序触发两次,那么您的处理程序可能会被注册两次。 您可以在注册处理程序之前添加一些日志记录,以validation这是发生了什么。 然后你可以调试它被注册两次的原因。