我在 iPad Safari 上安装了 jQuery Mobile,由于某种原因,触摸滑动事件会触发两次。
就在本周,人们在过去的一年中报告了同样的问题,但我找不到如何在不修改 jQuery Mobile 的情况下修复双重事件的解释,我不想这样做。jQuery 论坛上的主题 http://forum.jquery.com/topic/double-event-firing-on-swipeleft-and-swiperight-events
滑动处理程序的以下元素绑定都具有相同的不正确双事件结果,其中每次滑动都会调用警报两次。
jQuery Mobile 触摸事件应该如何绑定以避免双重冒泡?
// Test 1: Binding directly to document with delegate()
$(document).delegate(document, 'swipeleft swiperight', function (event) {
alert('You just ' + event.type + 'ed!');
});
// Test 2: Binding to document with on() handler recommended as of 1.7 with and without preventDefault
$(document).on('swipeleft',function(event, data){
event.preventDefault();
alert('You just ' + event.type + 'ed!');
});
// Test 3: Binding to body with on() with and without event.stopPropagation
$('body').on('swipeleft',function(event, data){
event.stopPropagation();
alert('You just ' + event.type + 'ed!');
});
// Test 4: Binding to div by class
$('.container').on('swipeleft',function(event, data){
event.stopPropagation();
alert('You just ' + event.type + 'ed!');
});
event.stopImmediatePropagation() http://api.jquery.com/event.stopImmediatePropagation/这就是技巧,它与 stopPropagation() 不同。确保jQuery on() http://api.jquery.com/on/在 document.ready 中调用方法似乎有帮助。我能够使用任何元素选择器来绑定事件,包括使用向上滑动和向下滑动here http://developingwithstyle.blogspot.com/2010/11/jquery-mobile-swipe-up-down-left-right.html
$(document).ready(function(){
$(document).on('swipeleft swiperight swipedown swipeup',function(event, data){
event.stopImmediatePropagation();
console.log('(document).Stop prop: You just ' + event.type + 'ed!');
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)