9.冒泡(event bubble)
缺省情况下,当几个元素重合,事件从最上层元素开始往下层元素传播。
例 9.1(bubbleIEFF.html)
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function docClick(event)
{
var event = event || window.event;
alert("IE和FF一样:外面文档:"+event.type);
}
//document.captureEvents(Event.CLICK);
document.onclick = docClick;
</script>
<div onClick="var event = event || window.event;alert('里面div:'+event.type);">ie和FF都冒泡从里往外</div>
马克-to-win:缺省情况下,当几个元素嵌套重合,事件从最上层元素开始往下层元素传播。
例 9.2(bubble1currentTargetsrcElementIEFF.html)
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function gotClick(event, who) {
/*
1) 在ie中没有currentTarget,我们只能用下面的方法:来仿真火狐的效果。
2)马克-to-win:event.target是最上层的元素,event.currentTarget是最底层的元素
*/
var event = event||window.event;
var eTarget =event.srcElement||event.target;
var eCurrentBottomTarget =event.srcElement||event.currentTarget;
if (window.navigator.userAgent.indexOf("MSIE") >= 1)
{
//var en=eCurrentBottomTarget.nodeName.toLowerCase();
// var whol=who.toLowerCase();
while( eCurrentBottomTarget.nodeName.toLowerCase()!=who.toLowerCase()){
eCurrentBottomTarget=eCurrentBottomTarget.parentNode;
// eCurrentBottomTarget=ep;
//en=eCurrentBottomTarget.nodeName.toLowerCase();
// var zhanwei = "zhanwei";
}
}else{
alert(event.target.id + " is event.target.id "+event.currentTarget.id+" is event.currentTarget.id");
}
alert(who +" and " + eTarget.id+" and "+eCurrentBottomTarget.id);
// var zhanwei = "zhanwei";
}
更多请见:http://www.mark-to-win.com/tutorial/js_4_eventbubble.html