JS阻止右键菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{margin: 0px;padding: 0px;}
#menu{width: 100px;height: 100px;background-color: gray;display: none;position: absolute;}
</style>
<script>
window.onload = function(){
document.oncontextmenu = function(){
return false;
}
var oMenu = document.getElementById("menu");
document.onmousedown = function(ev){
var e = ev || window.event;
if(e.button == 2){
oMenu.style.left = e.clientX + 'px';
oMenu.style.top = e.clientY + 'px';
oMenu.style.display = 'block';
}else{
oMenu.style.display = 'none';
}
}
}
</script>
</head>
<body>
<div id = 'menu'>
<ul>
<li>菜单1</li>
<li>菜单2</li>
<li>菜单3</li>
<li>菜单4</li>
</ul>
</div>
</body>
</html>
实现效果:
相关知识:
事件类型的种类
鼠标事件对象的属性