How do I prevent a click handler being triggered when a child element is clicked?

前端 未结 3 1782
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 01:36

I have two div tags, first div is the father and the second div is son Inside the father like this

3条回答
  •  时光说笑
    2020-12-07 02:03

    You can pass the event as one of the arguments in the closeFather function, then check whether the event target is the father element. See this example

    function closeFather(e) {
        if(e.target == document.getElementById('father')) {
            //do stuff
        }
    };
    

    Then in the HTML you just need to add the event argument to the javascript function.

提交回复
热议问题