js如何在页面禁止右击

JavaScript07

js如何在页面禁止右击,第1张

在THML页面代码添加以下代码就可以

<script language="javascript">

/*document.oncontextmenu=Youji*/ //为当前文档添加鼠标右击事件,防止默认的右击菜单弹出

function Youji()

{

alert("右击失败!")

return false

}

//为某个dom元素添加鼠标右击事件

window.onload = function(){

document.getElementById("div1").oncontextmenu=Youji

}

</script>

<div id="div1" >

</div>

保存后打开。面就可以看到效果,

实现代码如下:

<html oncontextmenu="doNothing()">

<head>

<title>屏蔽掉鼠标右键</title>

<script type="text/javascript">

function doNothing(){

window.event.returnValue=false

return false

}

</script>

</head>

<body oncontextmenu="doNothing()">

<div style="width:30pxheight:30pxbackground-color:#12aded"></div>

</body>

</html>