识别click元素并在元素为<a>

时间:2016-05-20 15:31:56

标签: javascript html css onclick

The html section lick

<div id="aaa">
 ...<a id="a1">...</a>...<a id="a2">...</a>...
</div>

I want to add a click event to handle the click of div. But all elements a have their click event handler. And the click event handler didn't stoppropagation, so it also will execute the div click event. And I cannot edit the element a's event handler and add stoppropagation().

How can I identify the click source element in the div's event handler? If it's an element a, I just return and do nothing.

1 个答案:

答案 0 :(得分:1)

如果是,则可以检查click事件目标标记名是否为锚返回。

document.getElementById('aaa').addEventListener("click", function(e){
  if(e.target.tagName.toLowerCase() === "a") return;
});