iOS bind javascript function to clicking on <html>

后端 未结 2 473
心在旅途
心在旅途 2021-01-28 16:15

This works in desktop safari, but in the iOS version no alert pops up. Is it possible to bind to the \'html\' element in iOS? I want to close a drop-down menu whenever the user

相关标签:
2条回答
  • 2021-01-28 16:25

    Use document or body element instead of html

        $(document).bind("click", function(){
              alert("clicked!")
        });
    

    You can also try this.

        $('html, body').bind("click", function(){
              alert("clicked!")
        });
    
    0 讨论(0)
  • 2021-01-28 16:28

    jQuery.click() doesn't function properly on iOS. I suspect that you're having the same problem with bind(). jQuery.delegate() however does work.

    $('html').delegate('body', 'click', function(){ alert("body"); });
    

    Should work for you. Presumably on() will also function, but I haven't had a chance to try it yet.

    As an aside, you should move that script block below the body tag and lose the $(document).ready().

    Update: In any recent vintage of jQuery, on(event, selector, function) is the way to go. It works on IOS and resists leaking event handlers.

    $('body').on('click', ':not[#stuff]', function(){ alert("not stuff"); });
    
    0 讨论(0)
提交回复
热议问题