Mobile Safari preventDefault() not working? Androi

2019-07-07 14:22发布

问题:

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