ng-click on parent clicks through children

后端 未结 2 1349
再見小時候
再見小時候 2021-02-03 17:32

I have a

element which has another
element as its child.

I added the ng-click directive to the parent, an

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-03 17:37

    You have to cancel event propagation, so the click event of the parent element won't get called. Try:

    You can click through me

    When you click the child element, its event gets triggered. But it doesn't stop there. First the click event of the child element is triggered, then the click event of the parent element gets triggered and so on. That's called event propagation, To stop event propagation (triggering of the parents click events), you have to use the above function, stopPropagation.


    Working example

    I added some CSS padding, so the example is clearer. Without padding the child element takes up the whole inner space and you can not click on the parent without clicking on the child.

提交回复
热议问题