Mobile Safari preventDefault()不起作用? Android运行正常 [英] Mobile Safari preventDefault() not working? Android works fine

查看:142
本文介绍了Mobile Safari preventDefault()不起作用? Android运行正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的目标在Chrome上完美无缺 - Android 4.1
但在iOS上却很难实现。

  $(document).on('mouseenter touchend','[id * = mmlink]',function(e){ 
var $ btn = $(this);
var href = $ btn.attr('href');
var count =($ btn.data('click_count')|| 0 )+ 1;

$ btn.data('click_count',count);
if(count == 1){
$ btn.click(function(v){
v.preventDefault();
});
} else {
document.location.href = href;
}
});

我使用milonic菜单生成子菜单。我需要使用 .on()来选择子菜单。



测试页: http://www.wolfbariatrics.com/mmtest/index.htm



我认为还有另一个事件只发生在iOS中。用于safari的远程调试器允许我设置断点,但是一旦我进入或超过它就跟随锚标记。



我已经完全删除了锚标记中的所有事件和href,但仍然无效。

解决方案

您可能想在StackOverflow上查看关于event.preventDefault的这个主题并返回false:



event.preventDefault()vs. return false



基本上:
jQuery的preventDefault不会阻止其他人执行。这就是stopImmediatePropagation的用途。





在jQuery事件处理程序中返回false实际上与在传递的jQuery.Event对象上调用e.preventDefault和e.stopPropagation相同。 / p>

e.preventDefault()将阻止发生默认事件,e.stopPropagation()将阻止事件冒泡并返回false将同时执行这两种操作。请注意此行为与普通(非jQuery)事件处理程序不同,其中,返回false不会阻止事件冒泡。


What i'm trying to accomplish works perfectly on Chrome -- Android 4.1 But fails pretty hard on iOS.

$(document).on('mouseenter touchend','[id*=mmlink]', function (e) {
    var $btn = $(this);
    var href = $btn.attr('href');
    var count = ($btn.data('click_count') || 0) + 1;

    $btn.data('click_count', count);
    if (count == 1) {  
        $btn.click(function(v) { 
            v.preventDefault();
        });
     } else {
        document.location.href = href;
     }
 });

I use milonic menu to generate sub menus. I need to use .on() to select the submenus.

test page: http://www.wolfbariatrics.com/mmtest/index.htm

I'm thinking there is another event that only happens in iOS. Remote Debugger for safari allows me to set breakpoints but as soon as I step in or over it follows the anchor tag.

I've gone as far as removing all events from the anchor tag entirely and the href but still nothing works.

解决方案

You might want to check this topic on StackOverflow about event.preventDefault and return false:

event.preventDefault() vs. return false

Basically: "jQuery's preventDefault does not prevent other handers from executing. That's what stopImmediatePropagation is for."

and

"return false from within a jQuery event handler is effectively the same as calling both e.preventDefault and e.stopPropagation on the passed jQuery.Event object.

e.preventDefault() will prevent the default event from occuring, e.stopPropagation() will prevent the event from bubbling up and return false will do both. Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up. "

这篇关于Mobile Safari preventDefault()不起作用? Android运行正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆