禁止鼠标右键的几种方法


第一种:常用方法
<script language="javascript">
<!--
if (window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu()
{
event.cancelBubble = true
event.returnvalue = false;
return false;
}
function norightclick(e)
{
if (window.Event)
{
if (e.which == 2 || e.which == 3)
return false;
}
else
if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true
event.returnvalue = false;
return false;
}
}
document.oncontextmenu = nocontextmenu;  // for IE5+
document.onmousedown = norightclick;  // for all others
//-->
</script>


第二种:笨方法

把下列代码加入到<head>与</head>之间
<SCRIPT language=javascript>
  function click() {
  if (event.button==2) { // event.button==1 禁止鼠标左键
  alert('禁止右键')
  }
  }
  document.οnmοusedοwn=click
  </SCRIPT>

第三种:添加至收藏夹

<SCRIPT language=JavaScript><!--
function click() {
if (event.button==2) {window.external.addFavorite('http://www.phpx.com','中国PHP联盟')}}
document.οnmοusedοwn=click
// --></SCRIPT>

第四种:使鼠标右键无响应。 仅 IE5 适用,取消菜单显示

把<BODY>改成<BODY οncοntextmenu=self.event.returnValue=false>


第五种:鼠标失效 适用IE

οndragstart="window.event.returnValue=false"
οncοntextmenu="window.event.returnValue=false"
onselectstart="event.returnValue=false"
把上面源代码复制到<body>中就可以锁定鼠标,让访客无法选定网页内容进行复制。即使从IE浏览器“编辑”菜单下选取“全选”都没用哟。


第六种:最聪明的方法,自己定制鼠标的右键菜单,给访问者以全新的感受!

第一步:把代码加入到<head>与</head>之间
<style>
<!--
/*
Context menu Script-
c Dynamic Drive
Last updated: 99/09/16th
For full source code, 100's more DHTML scripts, and Terms Of Use,
visit dynamicdrive.com
*/
#ie5menu{
position:absolute;
width:200px;
border:2px solid black;
background-color:menu;
font-family:Verdana;
line-height:20px;
cursor:default;
visibility:hidden;
}
.menuitems{
padding-left:15px;
padding-right:15px;
}
-->
</style>
<script language="JavaScript1.2">
//set this variable to 1 if you wish the URLs of the highlighted menu to be displayed in the status bar
var display_url=0
function showmenuie5(){
var rightedge=document.body.clientWidth-event.clientX
var bottomedge=document.body.clientHeight-event.clientY
if (rightedge<ie5menu.offsetWidth)
ie5menu.style.left=document.body.scrollLeft+event.clientX-ie5menu.offsetWidth
else
ie5menu.style.left=document.body.scrollLeft+event.clientX
if (bottomedge>ie5menu.offsetHeight)
ie5menu.style.top=document.body.scrollTop+event.clientY-ie5menu.offsetHeight
else
ie5menu.style.top=document.body.scrollTop+event.clientY
ie5menu.style.visibility="visible"
return false
}
function hidemenuie5(){
ie5menu.style.visibility="hidden"
}
function highlightie5(){
if (event.srcElement.className=="menuitems"){
event.srcElement.style.backgroundColor="highlight"
event.srcElement.style.color="white"
if (display_url==1)
window.status=event.srcElement.url
}
}
function lowlightie5(){
if (event.srcElement.className=="menuitems"){
event.srcElement.style.backgroundColor=""
event.srcElement.style.color="black"
window.status=''
}
}
function jumptoie5(){
if (event.srcElement.className=="menuitems")
window.location=event.srcElement.url
}
</script>
第二步:把下列代码加入到<body>与</body>之间
<!--[if IE]>
<div id="ie5menu" onMouseover="highlightie5()" onMouseout="lowlightie5()" onClick="jumptoie5()">
<div class="menuitems" url="http://www.phpx.com">中国PHP联盟</div>
<div class="menuitems" url="http://www.phpx.com">中国PHP联盟</div>
<div class="menuitems" url="http://www.phpx.com">中国PHP联盟</div>
<div class="menuitems" url="http://www.phpx.com">中国PHP联盟</div>
<div class="menuitems" url="http://www.phpx.com">中国PHP联盟</div>
<hr>
<div class="menuitems" url="http://www.phpx.com">中国PHP联盟</div>
</div>
<![endif]-->
<script language="JavaScript1.2">
document.οncοntextmenu=showmenuie5
if (document.all&&window.print)
document.body.οnclick=hidemenuie5
</script>

转载于:https://www.cnblogs.com/ioricc/archive/2005/10/27/263242.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值