js如何在页面禁止右击

JavaScript012

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>

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

方法:

function stop(){

   return false

  }

document.oncontextmenu=stop

但有时,输入框的右键不能屏蔽,可以尝试下面的脚本:

if (document.layers) 

{

document.captureEvents(Event.MOUSEDOWN)

}

document.onmousedown = click

document.oncontextmenu = new Function("return false")

function click(e) 

{

  e = e || event

  if (e.button == 2) 

{

    var tag = e.srcElement || e.target

   if (tag.type == "text" || tag.type == "textarea") 

    {

        document.oncontextmenu = new Function("return true")

     }

      else 

      {

      document.oncontextmenu = new Function("return false")

      }

  }

}