javascript——自定义右键菜单
CSS部分:
#div1{ width: 100px; height: 200px; border: 1px solid #333; position: absolute; display: none;}
HTML部分:
<div id="div1"></div>
JS部分:
window.onload = function(){ var oDiv = document.getElementById('div1'); //自定义右键菜单 document.oncontextmenu = function(ev){ var ev = ev || event; oDiv.style.display = 'block'; oDiv.style.left = ev.clientX + 'px'; oDiv.style.top = ev.clientY + 'px'; return false; //阻止右键菜单的默认行为 }; document.onclick = function(){ oDiv.style.display = 'none'; //点击文档隐藏右键菜单 }; };