Posted in Javascript onDecember 31, 2012
event.currentTarget identifies the current target for the event, as the event traverses the DOM. It always refers to the element the event handler has been attached to as opposed to event.target which identifies the element on which the event occurred.
即,event.currentTarget指向事件所绑定的元素,而event.target始终指向事件发生时的元素。翻译的不专业,好拗口啊,还是直接上测试代码吧:
<div id="wrapper"> <a href="#" id="inner">click here!</a> </div> <script type="text/javascript" src="source/jquery.js"></script> <script> $('#wrapper').click(function(e) { console.log('#wrapper'); console.log(e.currentTarget); console.log(e.target); }); $('#inner').click(function(e) { console.log('#inner'); console.log(e.currentTarget); console.log(e.target); }); /* 以上测试输出如下: 当点击click here!时click会向上冒泡,输出如下: #inner <a href="#" id="inner">click here!</a> <a href="#" id="inner">click here!</a> #wrapper <div id="wrapper">…</div> <a href="#" id="inner">click here!</a> 当点击click here!时click会向上冒泡,输出如下: #wrapper <div id="wrapper">…</div> <div id="wrapper">…</div> */ </script>
event.currentTarget与event.target的区别介绍
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@