在addEventListener或element.on附加的事件处理程序中返回false` [英] `return false` in an event handler attached by addEventListener or element.on*

查看:148
本文介绍了在addEventListener或element.on附加的事件处理程序中返回false`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,让我们先解决这个问题。是的,我想隐藏上下文菜单。不,我不是想阻止某人从我的页面上取消内容。它的用途是输入浏览器中的游戏,它将仅限于网页上的特定区域。

Right let’s get this out the way first. Yes, I want to hide the context menu. No, I’m not trying to prevent someone lifting content off my page. Its intended use is input for an in-browser game and it will be limited to a specific area on the webpage.

从意识形态转向技术...... / p>

Moving from the ideological to the technical...

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

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

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

Could someone explain to me why the addEventListener version is unable to stop the context menu from firing? The only difference I was able to see in Safari's Web Inspector was that document.onmousedown had a isAttribute value that was true whilst the addEventListener version had the same value as false.

推荐答案

所以我的无用搜索突然变得富有成效。

So my unfruitful search suddenly became fruitful.

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

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

适用于 Safari Firefox 歌剧 preventDefault()停止发生通常的操作。我不得不改变为了适应 Safari 而被监听的事件,无论如何它更合乎逻辑。更多信息:实现 EventListener 不应返回值,因此 return false 无效。

Works for Safari, Firefox, Opera. preventDefault() stops the usual actions from happening. I had to change the event that was listened for to accommodate for Safari and it is more logical anyway. Further information: functions that implement EventListener shouldn’t return values so return false had no effect.

这篇关于在addEventListener或element.on附加的事件处理程序中返回false`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆