admin管理员组文章数量:1314292
$('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);
}
});
本文标签:
版权声明:本文标题:javascript - $('body').on('click', '.anything', function(){}) not working on iOS 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741857310a2401420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论