Use the oncontextmenu
event.(使用oncontextmenu
事件。)
Here's an example:(这是一个例子:)
<div oncontextmenu="javascript:alert('success!');return false;">
Lorem Ipsum
</div>
And using event listeners:(并使用事件监听器:)
el.addEventListener('contextmenu', function(ev) {
ev.preventDefault();
alert('success!');
return false;
}, false);
Don't forget to return false, otherwise the standard context menu will still pop up.(不要忘记返回false,否则标准上下文菜单仍会弹出。)
If you are going to use a function you've written rather than javascript:alert("Success!")
, remember to return false in BOTH the function AND the oncontextmenu
attribute.(如果你打算使用你编写的函数而不是javascript:alert("Success!")
,请记住在函数和oncontextmenu
属性中返回false。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…