jquery-mobile - JQM : taphold event does not prevent click event

标签 jquery-mobile jquery-events

我正在尝试在 JQM 下的链接上绑定(bind) taphold 事件。

出现的问题是,如果用户长按鼠标并在此链接上松开鼠标 - 也会触发点击事件。当然不是故意的 - 我想分开这些事件。

所以我有一个这样的链接:

<a href="do-smth">link</a>

还有其他的东西,应该长按完成

$("a").on("taphold", function(event) {do_smth_else()});

当用户在链接上长按并释放鼠标时,do-smthdo_smth_else 都会执行。

在这里http://jsfiddle.net/GardenOfEmuna/68r4gph6/1/就是例子。

有没有人对此有补救措施?

最佳答案

据我所知,除了preventing the default behavior 别无他法。的 vclick事件:

$(".link").on({
    taphold: function() {
        post('long tap fired');
    },
    vclick: function(e) {
        e.preventDefault();
    }
});

这样,taphold 处理程序将被调用,但与链接关联的默认操作将被取消。

你会发现一个更新的 fiddle here .

如果您希望链接的默认行为仍然发生,但前提是没有执行长按,则必须将状态与元素本身相关联。 jQuery 的 data() facility 允许你写:

$(".link").on({
    taphold: function() {
        post('long tap fired');
        $(this).data("longTapRegistered", true);
    },
    "vclick vmouseout": function(e) {
        var $this = $(this);
        if (e.type == "vclick" && $this.data("longTapRegistered")) {
            e.preventDefault();
        }
        $this.removeData("longTapRegistered");
    }
});

请注意,我们还必须删除 vmouseout 上的持久化状态以避免您在评论中描述的问题。

你会发现一个 fiddle 演示这个 here .

关于jquery-mobile - JQM : taphold event does not prevent click event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25364868/

相关文章:

javascript - 动态样式 ListView

jquery-mobile - Tablesorter 寻呼机不工作

javascript - on.('click) 动态创建的元素在执行时被调用而无需单击

javascript - iPad : when using swipe event the entire page is moving

jquery - jQuery 可以确定哪些 div 当前位于用户的浏览器 View 中吗?

javascript - 动态加载 jQuery Mobile 导致 IE 最小化

javascript - 如何附加 document.click 事件而不接触某些元素?

javascript - 如何彻底破坏 swipe.js

javascript - 如何在 ReactJS 中创建、监听和触发自定义事件?

jQuery Mobile、DOM 和事件解除绑定(bind)