如何在JavaScript中禁用右键单击上下文菜单
问题描述:
并不是说我试图阻止查看源代码或类似的任何蠢事,但我正在为某些元素制作一些自定义上下文菜单。
Not that I'm trying to prevent 'View Source' or anything silly like that, but I'm making some custom context menus for certain elements.
编辑:对答案的回答:我试过这个:
response to answers: I've tried this:
<a id="moo" href=''> </a>
<script type="text/javascript">
var moo = document.getElementById('moo');
function handler(event) {
event = event || window.event;
if (event.stopPropagation)
event.stopPropagation();
event.cancelBubble = true;
return false;
}
moo.innerHTML = 'right-click here';
moo.onclick = handler;
moo.onmousedown = handler;
moo.onmouseup = handler;
</script>
答
捕获 onContextMenu
event,并在事件处理程序中返回false。
Capture the onContextMenu
event, and return false in the event handler.
您还可以捕获click事件并使用 event.button ,无论如何在某些浏览器中。
You can also capture the click event and check which mouse button fired the event with event.button
, in some browsers anyway.