在实际的前端WEB开发中,很多时间会做一些鼠标的操作。比如:限制用户对页面做出右键操作,设置用户右侧操作权限。
屏蔽鼠标右键操作要使用到oncontextmemu事件,IE浏览器中使用的returnValue属性等于false; 标准DOM的preventDefault()方法。
测试代码如下:
<html>
<head>
<title></title>
</head>
<script language="javascript" type="text/javascript">
//右击鼠标弹出提示信息。
function handle(oEvent)
{
if(window.event)
{
oEvent=window.event;
}
if(oEvent.button==2)
{
alert("鼠标不可用")
}
}
//屏蔽鼠标右键
function handle1(oEvent1)
{
if(window.event)
{
oEvent1=window.event;
oEvent1.returnValue=false;//取消右键默认
}
else
{
oEvent1.preventDefault();//取消右键默认
}
}
//document.οnmοusedοwn=handle;
document.οncοntextmenu=handle1;
</script>
<body>
<p>屏蔽右键</p>
</body>
</html>