W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <head> <style> div { background: yellow; border: 1px solid black; padding: 10px; } </style> </head> <body> <p>本例使用 HTML DOM 将 "oncontextmenu" 事件分配给 div 元素。</p> <div id="myDIV" contextmenu="mymenu"> <p>在此框中右键单击以查看上下文菜单! <menu type="context" id="mymenu"> <menuitem label="Refresh" onclick="window.location.reload();" icon="/i/ico_reload.png"></menuitem> <menu label="Share on..."> <menuitem label="Twitter" icon="/i/ico_twitter.png" onclick="window.open('//twitter.com/intent/tweet?text=' + window.location.href);"></menuitem> <menuitem label="Facebook" icon="/i/ico_facebook.png" onclick="window.open('//facebook.com/sharer/sharer.php?u=' + window.location.href);"></menuitem> </menu> <menuitem label="Email This Page" onclick="window.location='mailto:?body='+window.location.href;"></menuitem> </menu> </div> <p id="demo"></p> <script> document.getElementById("myDIV").oncontextmenu = function() {myFunction()}; function myFunction() { var x = document.getElementById("demo"); x.innerHTML = "You right-clicked inside div!"; x.style.fontSize = "30px"; } </script> <p><b>注释:</b>contextmenu 属性仅适用于 Firefox!</p> </body> </html>