$('body').on('click', '.anything', function(){}) 在 iOS 上不起作用
$('body').on('click', '.anything', function(){}) not working on iOS
$('body').on('click', '.anything', function(){})
与
$('.anything').click($.proxy(function(event) {});
我可以在安卓设备中使用$('body').on('click', '.anything', function(){})
执行点击功能,但在iOS设备中不起作用。实际上我正在使用科尔多瓦来开发我的应用程序
似乎在 iOS 上,如果浏览器没有遇到正式可点击的元素,则不会生成单击事件。例如A
标签总是生成点击事件,而body
或div
可能不会。
解决方法是设置 CSS 规则cursor: pointer;
该规则至少适用于某些元素。
所以你可以试试
body{
cursor: pointer;
}
或
.anything{
cursor: pointer;
}
在你的 CSS 中,看看它是否触发了。
最好对 Web 应用程序使用触摸事件。因为触摸事件比单击事件快。
尝试:
$('body').on('touchend', '.anything', function(){
console.log('don''t touch this. too do do do');
})
请使用ontouchend
不要使用onclick
。 onclick
无法在某些安卓设备上工作。
我在iOS应用程序上嵌入了WKWebView。我在WKWebView委托上监视js方法。
它不能按以下方式工作:
$('body').on('click', '#div', function(){})
它可以通过以下方式工作:
$('#div').click(function(event) {});
====
============================================附言:方法一:
<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>');
});
在这里,我们将 .click 方法附加到按钮,但如果您查看 html,该按钮尚不存在,因此我们将 .click 方法附加到一个不存在的元素。然后,按钮元素将被添加到 .test 类中,但它不会绑定 .click 方法。因此,如果您确实加载了此页面并尝试单击测试按钮,它将不会执行任何操作。
方法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>');
});
这将注销"它有效"。
不是我的解决方案,但它效果很好...
$(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);
}
});
相关文章:
- jQuery ContextMenu事件在IOS 8.2中不起作用
- Javascript touchend(滚动)事件在ios 4中的UIWebView中不起作用
- Ajax 调用在 IOS 中的用户交互之前不起作用
- window.open()在使用phone gap的iOS应用程序上不起作用
- Skrollr在IOS中不起作用,尽管i'We用Skrollr body包裹身体
- 删除类在 iOS 上不起作用
- Cordova iframe在iOS Safari中工作,但在Android中不起作用
- IOS Cordova/Phonegap 离线视频源标签不起作用
- <表单目标=“_blank”>在 iOS 7.0.3 中不起作用
- Jquery 函数在 ios 设备上不起作用
- 在 iFrame 中滚动事件侦听器在 iOS 上不起作用
- Vimeo 播放器 JS API 在 iOS 中不起作用
- 清除键盘打开时在iOS / iPad中不起作用的所有文本图标
- HTML
- 基于宽度计算的表格列显示和隐藏在 Safari IOS 设备中不起作用
- stringByEvaluatingJavaScriptFromString 在 iOS 7 中不起作用
- Ios webview调用javascript函数将不起作用
- 使用 ATTR 在 iOS 5 上不起作用更改 IMG 源
- Cordova iOS Javascript不起作用
- window.scroll到在iOS chrome中不起作用