最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $('body').on('click', '.anything', function(){}) not working on iOS

programmeradmin0浏览0评论
$('body').on('click', '.anything', function(){})

vs

$('.anything').click($.proxy(function(event) {});

I'm able to perform click function using $('body').on('click', '.anything', function(){}) in android device but the same is not working in iOS device. Actually I'm using cordova to develop my application

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

vs

$('.anything').click($.proxy(function(event) {});

I'm able to perform click function using $('body').on('click', '.anything', function(){}) in android device but the same is not working in iOS device. Actually I'm using cordova to develop my application

Share Improve this question edited Jan 14, 2016 at 12:22 jcubic 66.7k58 gold badges249 silver badges453 bronze badges asked Jan 14, 2016 at 10:22 muktamukta 791 silver badge7 bronze badges 1
  • Are .anything elements added after the code above? – dronus Commented Jan 14, 2016 at 10:28
Add a ment  | 

5 Answers 5

Reset to default 5

It seems on iOS, click events are not generated if the browser does not encounter an officially clickable element. So for example A tags always generate click events, while the body or divs may not.

An workaround is to set the CSS rule cursor: pointer; which works for at least some elements.

So you may try

body{
     cursor: pointer;
}

or

.anything{
     cursor: pointer;
}

in your CSS and see if it triggers then.

It is better to use touch events for web applications. Because touch events are faster than click event.

Try:

$('body').on('touchend', '.anything', function(){
   console.log('don\'t touch this. too do do do');
})

Please use ontouchend do not use onclick. onclick can not work on some android devices.

I inset a WKWebView on my iOS app. I monitor js method on WKWebView Delegate.

It can't work the following way :

$('body').on('click', '#div', function(){})

It can work the following way :

$('#div').click(function(event) {});

==========================================

ps: Method 1:

<html>
<body>
    <div class="test"></div>
    <script src="bower_ponents/jquery/dist/jquery.js"></script>
    <script src="test3.js"></script>
</body>

$(document).ready(function(){
   $('button').click(function() {
      console.log("It worked");
   });
   $('.test').html('<button>Test</button>');
});

Here we are attaching the .click method to the button, but if you look at the html the button does not exist yet so we are attaching the .click method to an inexistent element. The button element would then be added to the .test class, but it would not have the .click method bound to it. So if you did load this page and tried to click on the test button it would not do anything.

Method 2:

<html>
<body>
    <div class="test"></div>
    <script src="bower_ponents/jquery/dist/jquery.js"></script>
    <script src="test3.js"></script>
</body>

$(document).ready(function(){
    $('body').on('click', 'button', function() {
        console.log("It worked");
    });
    $('.test').html('<button>Test</button>');
});

This would log out “It worked”.

Not my solution, but it works great ...

$(function() {

    // 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);

    }

});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论