自定义右键菜单(contextmenu)
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 5 <title>test05</title> 6 </head> 7 <body> 8 <div id="myDiv">Right click or Ctrl+click me to get a custom context menu.Click anywhere else to get the default context menu.</div> 9 <ul id="myMenu" style="position:absolute;visibility:hidden;background-color:silver;"> 10 <li><a href="http://www.nczonline.net">Nicholas' site</a></li> 11 <li><a href="http://www.wrox.com">Wrox site</a></li> 12 <li><a href="http://www.yahoo.com">Yahoo!</a></li> 13 </ul> 14 <script type="text/javascript" src="eventutil.js"></script> 15 <script type="text/javascript" src="test05.js"></script> 16 </body> 17 </html>
1 EventUtil.addHandler(window, "load", function(event) { 2 var div = document.getElementById("myDiv"); 3 4 EventUtil.addHandler(div, "contextmenu", function(event) { 5 event = EventUtil.getEvent(event); 6 EventUtil.preventDefault(event); 7 8 var menu = document.getElementById("myMenu"); 9 menu.style.left = event.clientX + "px"; 10 menu.style.top = event.clientY + "px"; 11 menu.style.visibility = "visible"; 12 }); 13 14 EventUtil.addHandler(document, "click", function(event) { 15 document.getElementById("myMenu").style.visibility = "hidden"; 16 }); 17 });