我正在尝试在 JQM 下的链接上绑定(bind) taphold 事件。
出现的问题是,如果用户长按鼠标并在此链接上松开鼠标 - 也会触发点击事件。当然不是故意的 - 我想分开这些事件。
所以我有一个这样的链接:
<a href="do-smth">link</a>
还有其他的东西,应该长按完成
$("a").on("taphold", function(event) {do_smth_else()});
当用户在链接上长按并释放鼠标时,do-smth
和 do_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/