IE NC通用的藏鼠标右键一法


 首先,对于Internet Explorer的隐藏鼠标右键的方法,可以说只要弄过网页的人可能知道要用 document.oncontextmenu=Function("return false;"); 不过对于Netscape,这个办法就并不能完全行的通。

通过实践,我发觉,在Internet Explorer和Netscape这两款浏览器中都存在某些元素,鼠标的右键在上边点击是没有反应。现在我就利用如此一点来隐藏鼠标右键。我们可以知道在Internet Explorer和Netscape这两款浏览器中<div>的滚动条上是弹不出右键菜单的,那我们就在右键事件中把我们定制的<div>元素just_hide_it移到鼠标的点击的位置来,这样就不可能弹出右键菜单了。

    源代码如下:

<HTML><head><meta http-equiv="Content-Type" content="text/HTML; charset=gb2312">
<!--
Edited by Renjian Zhou(Joo),Shanghai Jiao Tong University,Application Mathematic,Class F0007102.If you are interested in editing HTML pages,connected me by bombjet.joo@163.com.
-->
<title>
通用的藏鼠标右键
</title>
</head>
<body style="z-index:-1;">
<script language="javascript">
tmp=navigator.appVersion.toString();
IE=parseFloat(tmp.substring(tmp.indexOf("MSIE")+4,tmp.length));
function NC_rightclick(e)
{
if(e.button==2 || (e.button==0 && e.type=="contextmenu"))
{
  document.getElementById('just_hide_it').style.left=e.clientX-14;
  document.getElementById('just_hide_it').style.top=e.clientY-14;
  return false;
}
}
if(IE)
document.oncontextmenu=Function("return false;");
else
{
document.write('<div id="just_hide_it" style="z-index:-1;position:absolute;left:100;top:0;overflow:scroll;width:28;height:28;"></div>');
document.oncontextmenu=NC_rightclick;
document.onmousedown=NC_rightclick;
}
</script>
</body>
</HTML>


    解释一下:不仅在Netscape中可以用如此<div>方法,在Internet Explorer中<div>方法也有效,不过既然在Internet Explorer中已经有了更有效的方法,我也就不用如此方法了。在Netscape中对just_hide_it要求z-Index为-1与body的z-index相同是出于使just_hide_it透明化,使人感觉不出有如此一个元素的存在。

    再说一句,我们隐藏鼠标右键的目的不是藏源代码,而是为了一些其他的网页的功能。顺便提倡各位大虾,有好的代码,多点共享。毕竟这个世界应该是一个共享的世界。

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3