首页 > 代码库 > js oncontextmenu阻止右键默认菜单 默认行为

js oncontextmenu阻止右键默认菜单 默认行为

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
*{
	margin:0px;
	padding:0px;
}
#div1{
	position:absolute;
	width:200px;
	
	background:#F00;
	color:#FFF;
	display:none;
	

}
</style>
<script>
//点击右键出现自定义菜单 document.oncontextmenu=function(ev){ var oEvent= ev||event; var oUl=document.getElementById("div1"); oUl.style.display="block"; oUl.style.left=oEvent.clientX+‘px‘; oUl.style.top=oEvent.clientY+‘px‘; return false; } document.onclick=function(){ var oUl=document.getElementById("div1"); oUl.style.display="none"; } </script> </head> <body> <div id="div1"> <ul id="ul1"> <li>aaa</li> <li>bbb</li> <li>ccc</li> </ul> </div> </body> </html>  

普通写法 return false

输入框只能输入数字

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload=function(){
	var oTxt=document.getElementById(‘tex1‘);
	oTxt.onkeydown=function(ev){
		var oEvent= ev||event;
		//alert(oEvent.keyCode);
		if(oEvent.keyCode!=8&&(oEvent.keyCode<49||(57<oEvent.keyCode&&oEvent.keyCode<96)||oEvent.keyCode>105)){
			return false;
			};
		};
	};
</script>
</head>

<body>
<input type="text" id="tex1">
</body>
</html>

  

js oncontextmenu阻止右键默认菜单 默认行为