我需要使用事件委託來捕獲帶有圖像的錨節點。使用javascript冒泡/捕獲檢測中間節點
document.addEventListener(
'click',
function(event) {
console.log(event.target);
return true;
},
false
);
<a href="#" class="link">
<img src="http://placehold.it/100x100" alt="">
</a>
event.target總是IMG。
如何檢查點擊是否在類爲.link的節點上進行?
UPDATE:要清楚這裏是an example with jQuery
當我使用jQuery.on()有a
節點this
財產回調溫控功能,不img
節點。使用純JS,我只能確定初始目標。
[什麼是事件冒泡和捕獲?](http:// stackoverflow。com/questions/4616694/what-is-event-bubbling-and-capturing) – Christoph
@Christoph這個問題的範圍比這個範圍窄得多,它引用了一個特定的問題,而不是比較兩個方法。 – freginold
@freginold我意識到這一點,但我認爲答案非常徹底,可能有助於OP更好地理解這個概念。 OPs問題的簡短回答就是:「事件目標始終是您點擊的實際元素。」 ;) – Christoph