且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ng点击父级点击子级

更新时间:2023-02-05 14:20:09

您必须取消事件传播,因此不会调用父元素的click事件.试试:

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

<div class="outer" ng-click="toggle()">
    <div class="inner" ng-click="$event.stopPropagation()">You can click through me</div>
</div>

单击子元素时,将触发其事件.但这并不止于此.首先触发子元素的click事件,然后触发父元素的click事件,依此类推.这就是事件传播,要停止事件传播(触发父母点击事件),您必须使用上面的功能stopPropagation.

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.

我添加了一些CSS填充,因此示例更加清晰.如果不填充,子元素将占据整个内部空间,并且如果不单击子元素,就无法单击父元素.

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.