Mobile Safari preventDefault() not working? Androi

2019-07-07 14:17发布

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条回答
太酷不给撩
2楼-- · 2019-07-07 14:28

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

查看更多
登录 后发表回答