$('body').on('click', '.anything', function(){}) 在 iOS 上不起作用

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

本文关键字:不起作用 iOS click body on anything function      更新时间:2023-09-26
$('body').on('click', '.anything', function(){})

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

我可以在安卓设备中使用$('body').on('click', '.anything', function(){})执行点击功能,但在iOS设备中不起作用。实际上我正在使用科尔多瓦来开发我的应用程序

似乎在 iOS 上,如果浏览器没有遇到正式可点击的元素,则不会生成单击事件。例如A标签总是生成点击事件,而bodydiv可能不会。

解决方法是设置 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不要使用onclickonclick无法在某些安卓设备上工作。

我在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);
    }
});
相关文章: