$ ('body'). on ('click', '.anything', function () {}) does not work on iOS

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

vs

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

I can execute the click function using $('body').on('click', '.anything', function(){})on the android device , but the same does not work on the iOS device . I actually use cordova to develop my application.

+4
source share
4 answers

It seems that on iOS, click events are not generated unless the browser encounters an officially dependent element. So, for example, tags Aalways generate click events, bodyor divthey may not be.

- CSS cursor: pointer;, .

,

body{
     cursor: pointer;
}

.anything{
     cursor: pointer;
}

CSS , .

+3

-. , click.

Try:

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

ontouchend, onclick. onclick Android.

0

WKWebView iOS. js- WKWebView Delegate.

:

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

:

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

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

: 1:

<html>
<body>
    <div class="test"></div>
    <script src="bower_components/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 attach the .click method to a button, but if you look at html, the button does not exist yet, so we bind the .click method to a nonexistent element. The button element will be added to the .test class, but it will not be bound to this .click method. Therefore, if you loaded this page and tried to click the test button, it did nothing.

Method 2:

<html>
<body>
    <div class="test"></div>
    <script src="bower_components/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 will exit the “It worked” system.

0
source

Source: https://habr.com/ru/post/1624247/


All Articles