1、event阻止默認(rèn)行為的方法為,如a標(biāo)簽:
$("a").click(function(event){
event.preventDefault();
});
3、event的兼容性寫(xiě)法:
在IE下event為window下的一個(gè)對(duì)象,所以應(yīng)寫(xiě)為window.event
//IE是把event事件對(duì)象作為全局對(duì)象window的一個(gè)屬性;可以使用event或window.event來(lái)訪問(wèn);
//FireFox和Chrome等主流瀏覽器是通過(guò)把【事件對(duì)象】作為【事件響應(yīng)函數(shù)】的【參數(shù)】進(jìn)入傳入的;
//兼容性的寫(xiě)法示例:
domElement.onclick = function( e ){
e = e || window.event;
}
注意:不要將var e=e||event; 寫(xiě)成 var e=event||e; ,這在FireFox下會(huì)提示錯(cuò)誤,F(xiàn)ireFox無(wú)法處理未聲明未賦值的變量event。
3、event.target的常用屬性總結(jié)如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#main {
width: 200px;
height: 100px;
background: pink;
color: #fff;
}
</style>
<script type="text/javascript">
window.onload = function() {
document.getElementById("main").onclick = function(e) {
console.log(e.target);//<div id="main" class="sb js node"><span>測(cè)試文字</span></div>
console.log(e.target.id);//main
console.log(e.target.tagName);//DIV
console.log(e.target.nodeName);//DIV
console.log(e.target.classList);// ["sb", "js", "node", value: "sb js node"]
console.log(e.target.className);//sb js node
console.log(e.target.innerHTML);//<span>測(cè)試文字</span>
console.log(e.target.innerText);//測(cè)試文字
}
}
</script>
</head>
<body>
<div id="main" class="sb js node"><span>測(cè)試文字</span></div>
</body>
</html>