ios – 停止jQuery Mobile滑动事件双重冒泡

我在iPad Safari上安装了jQuery Mobile,出于某种原因,触摸滑动事件发生了两次.

人们在本周报告了与本周相同的问题,但我无法找到解释如何在不修改jQuery Mobile的情况下修复双重事件,我不想这样做. Thread on jQuery forums

滑动处理程序的以下元素绑定都具有相同的不正确的双事件结果,其中每次滑动都会调用两次警报.

应该如何绑定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',data){
   event.stopPropagation();
   alert('You just ' + event.type + 'ed!');
});


// Test 4: Binding to div by class
$('.container').on('swipeleft',data){
   event.stopPropagation();
   alert('You just ' + event.type + 'ed!');
});

解决方法

event.stopImmediatePropagation()是诀窍,与stopPropagation()不同.确保在document.ready中调用 jQuery on()方法似乎有所帮助.我能够使用任何元素选择器绑定事件,包括使用swipeup和从 here向下滑动
$(document).ready(function(){    
    $(document).on('swipeleft swiperight swipedown swipeup',data){
        event.stopImmediatePropagation();
        console.log('(document).Stop prop: You just ' + event.type + 'ed!');
    });
});

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...