jQuery 移动停止 taphold 传播到 parent

jQuery Mobile stop taphold propagation to parent

我正在构建一个页面,我想在其中使用 taphold 来启动上下文菜单。但是,当元素位于 parent 内时,taphold 会传播到此 parent。我想阻止这种情况。

所以当用户按住 child 时,只有 child taphold 功能应该启动,而当用户按住 parent 时,只有 parent 应该启动。

我尝试了 el.stopPropagation() 和 el.stopImmediatePropagation(),但两者都没有阻止 taphold 传播到 parent。

This fiddle 会显示问题。按住绿色 child 也会触发红色 parent

的点击

fiddle显示两个div:

<div class='parent'>
   <div class='child'></div>
</div>

并使用此代码:

$('.parent').on('taphold', function( el ){
   alert( 'parent' );
});

$('.child').on('taphold', function( el ){
   alert( 'child' );
});

如果你点击并按住绿色 div 红色也会触发,这不是我想要的。

嗯,taphold不像其他事件那么容易,因为要识别它,需要启动一个timer,它应该检查经过的时间 来自初始屏幕压力(JQM 使用自己的 vmousedown 事件)。因此,在 tapholdThreshold 过去后 - 通常是 750 毫秒 - 框架可以引发 taphold 事件。

我相信可以获得 完美 解决方案您可能需要修补 JQM,否则这里有一个快速解决方法:

$('.parent').on('taphold', function( e ) {
    if(e.target == e.currentTarget) {
        // do the action
    }
});

无论如何都会引发 taphold,但是您可以捕获原点并跳过代码的执行。