首頁  >  文章  >  web前端  >  event.currentTarget與event.target的差異介紹_javascript技巧

event.currentTarget與event.target的差異介紹_javascript技巧

WBOY
WBOY原創
2016-05-16 17:44:461330瀏覽

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 elelement on which the eventopposed to event.target which.即,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="#" id="inner"> click here! <BR><a href="#" id="inner">click here! <BR>#wrapper <BR><div id= "wrapper">…</script>

click here!
點擊click here!時click會向上冒泡,輸出如下:
#wrapper


*/


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn