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

前端之家收集整理的这篇文章主要介绍了ios – 停止jQuery Mobile滑动事件双重冒泡前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在iPad Safari上安装了jQuery Mobile,出于某种原因,触摸滑动事件发生了两次.

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

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

应该如何绑定jQuery Mobile触摸事件以避免双重冒泡?

  1. // Test 1: Binding directly to document with delegate()
  2. $(document).delegate(document,'swipeleft swiperight',function (event) {
  3. alert('You just ' + event.type + 'ed!');
  4. });
  5.  
  6.  
  7. // Test 2: Binding to document with on() handler recommended as of 1.7 with and without preventDefault
  8. $(document).on('swipeleft',function(event,data){
  9. event.preventDefault();
  10. alert('You just ' + event.type + 'ed!');
  11. });
  12.  
  13.  
  14. // Test 3: Binding to body with on() with and without event.stopPropagation
  15. $('body').on('swipeleft',data){
  16. event.stopPropagation();
  17. alert('You just ' + event.type + 'ed!');
  18. });
  19.  
  20.  
  21. // Test 4: Binding to div by class
  22. $('.container').on('swipeleft',data){
  23. event.stopPropagation();
  24. alert('You just ' + event.type + 'ed!');
  25. });

解决方法

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

猜你在找的iOS相关文章