javascript - Jquery Tap and Touch 这么长的 Action

标签 javascript jquery mobile

我有一个 tabber,当我点击按钮时

$(document).on('click touchstart', 'element', function() { /*Some code*/ });

tabber 开关,如果手指区域有按钮或链接 - 它被激活。

如何避免这种影响?

提前致谢!

像这样的东西:jsfiddle.net/r99headp(在 Google Chrome 激活的设备工具栏中)

最佳答案

据我所知,我认为这与 touchstart 事件上的被动事件监听器和事件传播有关。当我尝试像这样将 prevenDefault 添加到您的偶数处理程序时:

$(function() {
    $(document).on('click touchstart', '.js-next', function(e){
        e.preventDefault();
        $('.js-tab').click();
    });
});

我在 chrome 中收到此警告:

Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

点击该链接,您将在文档中看到:

AddEventListenerOptions defaults passive to false. With this change touchstart and touchmove listeners added to the document will default to passive:true (so that calls to preventDefault will be ignored).

追下去我在 SO 上找到了这个答案:https://stackoverflow.com/a/39187679/1819684这表示 jQuery 在将选项传递给 addEventListener 时存在持续问题。这意味着目前您不能使用 jQuery 并设置 passive: false 来做到这一点。所以我将你的示例代码修改为:https://jsfiddle.net/w9h4an81/

$(function() {
    document.addEventListener('touchstart', function(e) {
        e.preventDefault();
        $('.js-tab').click();
    }, {passive: false});

    $(document).on('click', '.js-next', function(e){
        $('.js-tab').click();
    });
});

要只用 jQuery 来做,你必须做这样的事情:https://jsfiddle.net/6377wdhc/您不能在 document 上收听。

$(function() {
    $('.js-next').on('click touchstart', function(e){
        e.preventDefault();
        $('.js-tab').click();
    });
});

关于javascript - Jquery Tap and Touch 这么长的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43849949/

相关文章:

javascript - 在 node.js 中使用来自 HTTP 请求的数据

javascript - 删除下钻标签的下划线

javascript - 将字符串名称转换为javascript变量名称?

javascript - 如何操作浏览器历史记录并捕获浏览器后退/前进按钮?

css - 图标...调整大小或缩小?

javascript - 数组到对象 : declaring object inside vs outside for loop. 不同的结果

javascript - 将表单提交到新窗口/标签

javascript - jQuery append 后没有图像尺寸

python - 通过移动应用程序在 Raspberry Pi 上切换显示 OSMC 的电视

api - 发送短信进行号码验证最便宜的方式?