1 target在事件流的目标阶段;currentTarget在事件流的捕获,目标及冒泡阶段。
只有当事件流处在目标阶段的时候,两个的指向才是一样的;
而当处于捕获和冒泡阶段的时候,target指向被单击的对象而currentTarget指向当前事件活动的对象(一般为父级)。
2 HTML
<body>
<form id="form1" runat="server">
<div id="outer" style="background: #099">
click outer
<p id="inner" style="background: #9C0">
click inner</p>
<br>
</div>
<script language ="javascript">
var outer = G("outer");
var inner = G("inner");
addEvent(inner, "click", test);
addEvent(outer, "click", test);
</script>
</form>
</body>
3 javascript
<script language="javascript" defer="defer">
function G(id)
{
return document.getElementById(id);
}
function addEvent(obj, ev, handler)
{
if (window.attachEvent)
{
obj.attachEvent("on" + ev, handler);
}
else if (window.addEventListener)
{
obj.addEventListener(ev, handler, false);
}
}
function test(e)
{
alert("e.target.tagName : " + e.target.tagName + "\n e.currentTarget.tagName : " + e.currentTarget.tagName);
}
</script>
4 结果
点击 “inner”
先出现
`后出现:
e.target.tagname:P(冒泡阶段)
e.currentTarget.tagname:Div(冒泡阶段)
点击 "outer"
e.target.tagname:Div(目标阶段)
e.currentTarget.tagname:Div(目标阶段)
5 执行的部分脚本放在Dom 定义之后才能加载实现
var outer = G("outer");
var inner = G("inner");
6 可以<body onload =mytest();>
定义函数
function mytest()
{
var outer = G("outer");
var inner = G("inner");
addEvent(inner, "click", test);
addEvent(outer, "click", test);
}
7 如果用jQuery 可以这样:
$(function() {
var outer = G("outer");
var inner = G("inner");
addEvent(inner, "click", test);
addEvent(outer, "click", test);
})