jQuery .on() and .delegate() doesn't work on iPad

后端 未结 7 780
北荒
北荒 2020-12-05 04:38

If you try this snippet on desktop, everything works.
Whenever you try it on iPad, it won\'t do anything.

7条回答
  •  忘掉有多难
    2020-12-05 05:09

    i found a solution on http://www.danwellman.co.uk/fixing-jquery-click-events-for-the-ipad/

    do the following approach:

    var isIPad = function() {
        return (/ipad/i).test(navigator.userAgent);
    };
    var isIPhone = function() {
        return (/iphone/i).test(navigator.userAgent);
    };
    var isIPod = function() {
        return (/ipod/i).test(navigator.userAgent);
    };
    

    and where you bind the click-event-handler do so:

    var eventName = (isIPod() || isIPad() || isIPhone()) ? "touchstart" : "click";
    // if you are using jquery-mobile
    eventName = (isIPod() || isIPad() || isIPhone()) ? "touchstart" : "vclick";
    
    $(".selector").bind(eventName, function(e) {
        //do something here
    });
    // or
    $(document).on(eventName, ".selector", function(e) {
        //do something here
    });
    

    that's it.

提交回复
热议问题