运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>HTML DOM Events</h1> <h2>The oncontextmenu Event</h2> <p>Use the addEventListener() method to attach a "contextmenu" event to a div element.</p> <div id="div01" style="border:1px solid black; padding:10px"> <p>Right-click in this box to see the hidden content!</p> </div> <div id="myDiv" style="visibility:hidden"> <p>This info is hidden</p> <p>This info is hidden</p> <p>This info is hidden</p> </div> <script> // Assign an "contextmenu" event to div01: document.getElementById("div01").addEventListener("contextmenu", myFunction); // Prevent default context menu: const div = document.getElementById("div01"); div.addEventListener("contextmenu", (e) => {e.preventDefault()}); // Show hidden content: function myFunction() { const div = document.getElementById("myDiv"); div.style.visibility = "visible"; } </script> </body> </html>