.html
<div class="col col-50"
ng-repeat="col in row"
ng-click="openProductDetails(col.prod_id);">
<button
ng-click="addToCart(col.prod_id);">
Add to cart
</button>
</div>
如您所见,我在这里有一个grid
或<div>
,作为点击事件openProductDetails()
。在网格内部,我有一个button
,它有一个addToCart()
.问题是当我单击按钮时,openProductDetails()
也被触发。我找不到一种方法来防止单击按钮时父元素的单击事件。
提前谢谢。
您可以使用 event.stopPropation()
。此变量是对 JS 事件对象的引用,可用于调用 stopPropagation((
<div class="col col-50"
ng-repeat="col in row"
ng-click="openProductDetails(col.prod_id);">
<button
ng-click="addToCart(col.prod_id); $event.stopPropagation();">
Add to cart
</button>
</div>