【发布时间】:2020-12-04 22:21:04
【问题描述】:
我正在开发一个移动网站,并且在我不希望它们触发时遇到事件触发。
为了简单起见,它是这样写的(在 jQuery 中):
el.on('touchend', function(ev) {
ev.preventDefault();
// fire an ajax call
};
但是,有时用户会在滚动页面时点击该项目,从而触发 ajax 请求(从而改变页面状态)。
我想不出解决办法(ev.stopPropagation() 不起作用),所以我决定关注dragstart 和dragend 事件。
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 事件在我停止拖动后确实触发了。
有谁知道如何防止任何其他事件触发?我希望我只是瞎了眼,错过了一些明显的东西。
【问题讨论】:
标签: javascript jquery mobile jquery-events