如何防止拖拽后的触端事件

How to prevent touchend event after dragend?

本文关键字:事件 何防止      更新时间:2023-09-26

我正在一个移动网站上工作,并在我不希望它们触发时为事件触发而苦苦挣扎。

为了简单起见,它写了这样的东西(在jQuery中):

el.on('touchend', function(ev) {
    ev.preventDefault();
    // fire an ajax call
};

但是,有时用户会在滚动页面时点击该项目,从而导致 ajax 请求触发(从而更改页面状态)。

我想不出解决方法(ev.stopPropagation()不起作用),所以我决定关注dragstartdragend事件。

el.on('dragstart', function() {
     el.off('touchend');
});
el.on('dragend', function(ev) {
     ev.stopPropagation(); // <-- seems to do nothing
     // add back the event above (the function is set to a var)
});

如果我在touchend回调中发出警报,我会确认touchend事件在我停止拖动后确实触发了。

有谁知道如何防止任何其他事件触发?我希望我只是瞎了眼,错过了一些明显的东西。

您无法阻止触发事件,只能阻止该事件的默认行为发生。

如果您担心在触端事件触发之前页面的状态会发生变化,则只需更改函数以检查并考虑状态更改。

我认为在这种情况下,您应该使用

event.stopImmediatePropagation();