gpt4 book ai didi

javascript - `return false` 在由 addEventListener 或 element.on* 附加的事件处理程序中

转载 作者:搜寻专家 更新时间:2023-11-01 05:17:54 25 4
gpt4 key购买 nike

好吧,让我们先解决这个问题。是的,我想隐藏上下文菜单。不,我并不是要阻止有人从我的页面上删除内容。其预期用途是为浏览器内游戏输入,并且仅限于网页上的特定区域。

从意识形态转向技术......

var mouse_input = function (evt) {
// ...
return false;
}

document.onmousedown = mouse_input; // successful at preventing the menu.
document.addEventListener('mousedown', mouse_input, true); // unsuccessful

有人可以向我解释为什么 addEventListener 版本无法阻止上下文菜单触发吗?我在 SafariWeb Inspector 中看到的唯一区别是 document.onmousedown 有一个 isAttribute值为 true 而 addEventListener 版本的值为 false。

最佳答案

于是我毫无结果的寻找突然变得富有成果。

var mouse_input = function (evt) {
evt.preventDefault();
}

document.addEventListener('contextmenu', mouse_input, false);

适用于 SafariFirefoxOperapreventDefault() 阻止通常的操作发生。我不得不更改监听的事件以适应 Safari 并且无论如何它更合乎逻辑。更多信息:实现 EventListener 的函数不应返回值,因此 return false 无效。

关于javascript - `return false` 在由 addEventListener 或 element.on* 附加的事件处理程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3462123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com