.on(“click”) does not work on iOS

前端 未结 2 1172
醉酒成梦
醉酒成梦 2020-12-10 23:07

I noticed that

$(\"body\").on(\"click\", \"#id\", function(event) {...

does not work on iOS while

$(\"#id\").on(\"click\",          


        
相关标签:
2条回答
  • 2020-12-10 23:28

    Try as follows once:

    $(document).on("click touchstart", "#id", function(event) {...
    
    0 讨论(0)
  • 2020-12-10 23:30

    If you want to add click and not use touch events you can do this (although it does involve agent sniffing):

    // Check if it is iOS
    var isiOS = (navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false);
    
    if(isiOS === true) {
    
        // Store -webkit-tap-highlight-color as this gets set to rgba(0, 0, 0, 0) in the next part of the code
        var tempCSS = $('a').css('-webkit-tap-highlight-color');
    
        $('body').css('cursor', 'pointer')                                    // Make iOS honour the click event on body
                 .css('-webkit-tap-highlight-color', 'rgba(0, 0, 0, 0)');     // Stops content flashing when body is clicked
    
        // Re-apply cached CSS
        $('a').css('-webkit-tap-highlight-color', tempCSS);
    
    }
    

    See more at http://www.texelate.co.uk/blog/post/124-add-a-click-event-to-html-or-body-on-ios-using-jquery/

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