点击链接内的按钮

时间:2019-05-01 11:42:14

标签: javascript

我在链接块内部有js动作块,当用户用鼠标单击类import { NotificationComponent } from '../common/components/notification/notification.component'; constructor( private notification: NotificationComponent ) { // this.productCodesList=this.productCodesList1; this.selectedProductCode == '20201'; // this.productCodesList=this.productCodesList1; this.vehicleTypeList = this.twoWheelertype; this.selectedType = 'KFZK120'; this.getVehicleMake(); } this.notification.notification.success('Data is deleted'); 的块(请参见代码底部)时,就会触发函数child_container。如果将类为parent_link_trigger的元素移到外部,则会破坏很多逻辑,因此需要大量时间进行重构。 这是我的代码:

child_container

当鼠标单击<a class="parent_container" onclick="parent_link_trigger(); return false;" > <div><span>here some text</span> </div> <div class="child_container" onclick="children_link_trigger(); return false;"> </div> </a> 时,是否可以调用child_container onclick侦听器?

1 个答案:

答案 0 :(得分:2)

您必须使用子功能上的Event​.stop​Propagation()在捕获阶段和冒泡阶段中停止当前事件的进一步传播

function parent_link_trigger(){
  console.log('parent_link_trigger');
}
function children_link_trigger(e){
  e.stopPropagation();
  console.log('children_link_trigger');
}
<a class="parent_container" onclick="parent_link_trigger();" >
  <div><span>here some text</span>
  </div>
  <div class="child_container" onclick="children_link_trigger(event);">Child
  </div>
</a>

相关问题