DOM events target
and currentTarget
the difference
target
It is an event-triggered real elementcurrentTarget
Is the event binding elementEvent handler in
this
point is forcurrentTarget
currentTarget
Andtarget
, sometimes one and the same element, and sometimes not the same element (because the event bubbling)- When an event is triggered child element,
currentTarget
the element binding events,target
as child elements - When the event is triggered when the element itself,
currentTarget
andtarget
for the same element.
- When an event is triggered child element,
<body>
<ul id="box">
<Li id="apple">苹果</Li>
<li>香蕉</li>
<li>桃子</li>
</ul>
</body>
<script type="text/javascript">
var box = document.getElementById('box');
var apple = document.getElementById('apple');
//直接绑定在目标元素apple上
apple.onclick = function (e){
console.log(e.target); //<li id="apple">苹果</li>
console.log(e.currentTarget); //<li id="apple">苹果</li>
console.log(this); //<li id="apple">苹果</li>
console.log(e.target === e.currentTarget); //true
console.log(e.target === this); //true
}
//绑定在父元素box上(如果点击apple这个li时)
box.onclick = function (e){
console.log(e.target); // <li id="apple">苹果</li>
console.log(e.currentTarget); //<ul id="box">...</ul>
console.log(this); //<ul id="box">...</ul>
console.log(e.currentTarget===this); //true
console.log(e.target === e.currentTarget); //false
console.log(e.target === this); //false
}
</script>