阻止事件默认行为的几种方式

阻止事件默认行为的几种方式 欧嘉怡

阻止事件默认行为的几种方式

<a href="http://www.zxwyit.com/" id="guanwang" >志学无忧</a>

var caolj=document.querySelector("#guanwang")// 阻止超链接事件

caolj.addEventListener("click",function(){

if(event.preventDefault){

window. event.preventDefault()

}else{

event.returnValue=false

}

return false

})

<button id="guanwang1">右击菜单行为</button> 鼠标右击网页

var caolj1=document.querySelector("#guanwang1")

window.oncontextmenu=function(){//阻止右击菜单

alert("阻止菜单")

window.event.preventDefault()

}

<button id="guanwang2">阻止复制</button>

document.body.oncopy=function(e){ //禁止拷贝

alert("禁止拷贝")

window.event.preventDefault()

}

<button id="guanwang3">阻止粘粘</button>

var aaaa=document.getElementById("cccc")文本框

aaaa.onpaste=function(){ //禁止粘粘内容

alert("禁止粘粘")

window.event.preventDefault()

}

<button id="guanwang4">关闭窗口</button>

取消窗体的关闭事件

window.getSelection("#guanwang4")=function(){ //关闭窗口

return "确认关闭吗?"

}

还能输出{{restrictNumber}}个字符  
  • {{reply.author}}

    {{CommonUtil.formateDate(reply.ac_CommentDate).shortTime}}
  • 回复了{{Comments.author}} :