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

后端 未结 7 765
北荒
北荒 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 04:59

    It's a Safari mobile bug/feature : click events won't bubble all the way up to body.

    Adding onclick="" is a known workaround, but IMHO it's easier to attach your listener on a first child of <body>.

    See: http://www.quirksmode.org/blog/archives/2010/09/click_event_del.html

    0 讨论(0)
  • 2020-12-05 05:03

    On iOS there is no event bubbling without a cursor style.
    So in your CSS you need to add cursor: pointer; to the element.

    $('body').on('click', '#click', function() {
        alert("This alert won't work on iPad");
    });
    #click { 
      font-size: 24px; 
      cursor: pointer;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="click">Click here</div>

    I know it been asked a long time ago, but I thought a simple CSS solution might help.

    0 讨论(0)
  • 2020-12-05 05:03

    I was facing this issue on iPhone 5C and below.
    This worked for me:

    body {
      cursor:pointer;
    }
    
    0 讨论(0)
  • 2020-12-05 05:04

    I had an issue with a message that I prepend to the html body. I found this issue on [jQuery 'click' event doesn't work on iOS Safari?

    I used this

    $('body').on('click touchstart', 'someselect', function(){})
    

    And it worked on iPhone 4S and iPhone 5S

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-05 05:15

    Change the cursor style of the body on iOS to "pointer" and everything will work perfectly. You won't have to add onclick="" on every element you want clickable...

    <script type="text/javascript">
        $(function() {
            // The trick
            if (/ip(hone|od)|ipad/i.test(navigator.userAgent)) {
               $("body").css ("cursor", "pointer");
            }
            // The test
            $("body").on("click", "#click", function() {
                alert("This also works on iOS !");
            });
        });
    </script>
    <div id="click">Click here</div>
    

    I know what you're thinking right now: "WTF!".

    0 讨论(0)
提交回复
热议问题