首页 >web前端 >js教程 >event.currentTarget与event.target的区别介绍_javascript技巧

event.currentTarget与event.target的区别介绍_javascript技巧

WBOY
WBOY原创
2016-05-16 17:44:461359浏览

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始终指向事件发生时的元素。翻译的不专业,好拗口啊,还是直接上测试代码吧:

复制代码 代码如下:



<script> <BR>$('#wrapper').click(function(e) { <BR>console.log('#wrapper'); <BR>console.log(e.currentTarget); <BR>console.log(e.target); <BR>}); <BR>$('#inner').click(function(e) { <BR>console.log('#inner'); <BR>console.log(e.currentTarget); <BR>console.log(e.target); <BR>}); <BR>/* <BR>以上测试输出如下: <BR>当点击click here!时click会向上冒泡,输出如下: <BR>#inner <BR><a href=&#8203;"#" id=&#8203;"inner">&#8203;click here!&#8203;&#8203; <BR><a href=&#8203;"#" id=&#8203;"inner">&#8203;click here!&#8203;&#8203; <BR>#wrapper <BR><div id=&#8203;"wrapper">&#8203;…&#8203;</script>

​click here!​
当点击click here!时click会向上冒泡,输出如下:
#wrapper
​…​

​…​

*/

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn