浅谈JavaScript的事件
1、事件流
事件流描述的是从页面中接收事件的顺序。但是IE提出的是冒泡流,而NetscapeCommunicator提出的是捕获流。
JavaScript事件流
2、事件冒泡(eventbubbling)
事件开始由最具体的元素(嵌套层次最深的那个节点)接收,然后逐级向上传播为较不为具体的节点(文档)。如下:
<html> <head> <title>事件冒泡</title> </head> <body> <divid="myDiv">点击我</div> </body> </html> window.onload=function(){ varobj=document.getElementById("test"); obj.onclick=function(){ alert(this.tagName); }; document.body.onclick=function(){ alert(this.tagName); }; document.documentElement.onclick=function(){ alert(this.tagName); }; document.onclick=function(){ alert("document"); }; window.onclick=function(){ alert("window"); } };
事件传播顺序:div——>body——>html——>document
注意:
现代所有浏览器都支持冒泡事件,但实现还有一些差别。IE5.5及更早版本中的事件冒泡会直接从body跳到document(不执行html)。Firefox、Chrome和Safari则将事件一直冒泡到window对象。
3、停止事件冒泡和取消默认事件
a.获取事件对象
functiongetEvent(event){ //window.eventIE //event非IE returnevent||window.event; }
b功能:停止事件冒泡
functionstopBubble(e){ //如果提供了事件对象,则这是一个非IE浏览器 if(e&&e.stopPropagation){ //因此它支持W3C的stopPropagation()方法 e.stopPropagation(); }else{ //否则,我们需要使用IE的方式来取消事件冒泡 window.event.cancelBubble=true; } }
c.阻止浏览器的默认行为
functionstopDefault(e){ //阻止默认浏览器动作(W3C) if(e&&e.preventDefault){ e.preventDefault(); }else{ //IE中阻止函数器默认动作的方式 window.event.returnValue=false; } returnfalse; }