简体   繁体   English

如何将复选框onclick事件传递给父onclick?

[英]How can I pass checkbox onclick event to parent onclick?

I a with an onclick event and , and inside it. 我是一个onclick事件,并在其中。
When I click on the checkbox then the div-onclick event doesnt fire - I would like it to fire without having to dublicate the onclick onto the checkbox. 当我点击复选框时,div-onclick事件不会触发 - 我希望它可以触发,而不必将onclick复制到复选框上。

<div id="id-tag" onclick="do()">
  <span>text</span>
  <img src="pic.jpg" />
  <input type="checkbox" name="mycheck" />
</div>

I know the div-idtag but ideally would like to avoid specifying it inside the checkbox. 我知道div-idtag,但理想情况下我想避免在复选框中指定它。
Using another function call on the checkbox onclick to call the parent is NOT what I am looking for, like 使用复选框onclick上的另一个函数调用来调用父级不是我想要的,比如

onclick="run_parent_onclick()"

but this is okay (untested) 但这没关系(未经测试)

onclick="this.parent.click()"

Use addEventListener: 使用addEventListener:

function do(e) { ... }
document.getElementById('id-tag').addEventListener('click', do, true);

The "true" makes the event capture. “true”使事件捕获。

You could fire the div's click event manually in the input's onclick: 您可以在输入的onclick中手动触发div的click事件:

<input type="checkbox" name="mycheck" onclick="this.parentNode.click()" />

or 要么

<input type="checkbox" name="mycheck" onclick="document.getElementById('id-tag').click()" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM