Mobile Safari preventDefault() not working? Android works fine

放肆的年华 提交于 2020-01-04 03:51:29

问题


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.


回答1:


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. "



来源:https://stackoverflow.com/questions/12696459/mobile-safari-preventdefault-not-working-android-works-fine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!