$('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
-
Are
.anything
elements added after the code above? – dronus Commented Jan 14, 2016 at 10:28
5 Answers
Reset to default 5It 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 div
s 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);
}
});