使用触摸事件时保持菜单关闭

时间:2017-01-19 22:05:22

标签: javascript jquery-ui-contextmenu

我已经连接了一个简单的长触摸功能,在500ms之后使用“open”API命令打开上下文菜单。菜单打开。但是,在“touchend”上,菜单消失了。只有在“touchend”之前触摸上下文菜单时它才会停留。有没有办法防止这种行为?从源代码中,只有dom不同部分的“touchstart”才会触发close事件。

代码如下,如果有用的话。不是我的上下文菜单需要tr的委托 - 解释下面的targetTr变量使用。

var mobDevice_onLongTouch,
    mobDevice_touchTimer,
    mobDevice_longPressDuration = 500; //length of time we want the user to touch before we do something

//handle long press on the datatable
var touchArea = document.querySelector("#table");
touchArea.addEventListener("touchstart", touchAreaTouchStart, false);
touchArea.addEventListener("touchend", touchAreaTouchEnd, false);

function touchAreaTouchStart(e) {
    var targetTr = $(e.target).closest('tr');
    mobDevice_touchTimer = setTimeout(function () { touchArea_onLongTouch(targetTr) }, mobDevice_longPressDuration)
};
function touchAreaTouchEnd(e) {
    if (mobDevice_touchTimer) {
        clearTimeout(mobDevice_touchTimer) //reset the clock
    }
};

function touchArea_onLongTouch(target) {
    $('#table').contextmenu('open', target);
};

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 ContextMenu工作正常,但我正在触摸的DOM控件上注册了touchend上的更改事件(突出表格行)。因此,在触摸和保持期间弹出上下文菜单,然后通过touchend的DOM更改清除。

解决方案是手动将高亮表行事件添加到touchstart并在touchend上阻止默认(当触摸目标在表格内时)