可点击div中的可点击图像调用两个onclick处理程序

时间:2014-03-16 18:21:36

标签: javascript html

这是我的问题:

我有一个 img ,在 div 中有一个onclick处理程序,它还有一个onclick处理程序。

<div onclick="someFunction();">
  <img src="/Images/SomeImage.png" onclick="someOtherFunction();" />
</div>

点击图片时,我不想让div的处理程序被解雇。

我尝试添加&#34;返回false;&#34;在我的img的onclick结束时,无济于事。

enter image description here

我需要这样做的原因是因为我有一堆项目,在div上突出显示该条目,如果你点击它会显示更多细节,但你可能还想编辑/删除该条目。

谢谢!

1 个答案:

答案 0 :(得分:2)

使用stopImmediatePropagation()方法停止即时传播。

<强> HTML

  <img src="/Images/SomeImage.png" onclick="someOtherFunction(event);" />

javaScript

 //this function stops the propagation and not triggered parent div's handler
function someOtherFunction(event) {
    event.stopImmediatePropagation();
    return false;
}

DEMO

相关问题