利用contextmenu事件,自定义右键
Posted drafire
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用contextmenu事件,自定义右键相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript"> window.onload = function () { var div1 = document.getElementById("div1"); var ul1 = document.getElementById("ul1"); div1.oncontextmenu = function (event) { event.preventDefault(); var left = (event.clientX+10)+"px"; var right = (event.clientY-15)+"px"; ul1.hidden = false; ul1.style.left = left; ul1.style.top = right; } window.onclick = function (event) { ul1.hidden = true; } } </script> <style type="text/css"> ul { list-style: none; border: 1px red solid; background: #00ff90; position: absolute; padding:2px; } li { margin:20px 0px; } span { margin:5px 10px; display:block; text-align:center; } </style> </head> <body> <div style="width:100px;border:solid 1px blue" id="div1">点击我啊,菜鸡</div> <ul hidden="hidden" id="ul1"> <li><span>新浪微博</span></li> <li><span>QQ空间</span></li> <li><span>QQ</span></li> </ul> </body> </html>
以上是关于利用contextmenu事件,自定义右键的主要内容,如果未能解决你的问题,请参考以下文章