简体   繁体   English

元素中的Javascript / jQuery Click元素

[英]Javascript/jQuery Click Element within Element

This is probably something quite easy to resolve, but for some reason, I can't figure it out. 这可能很容易解决,但是由于某些原因,我无法弄清楚。

I have the following HTML: 我有以下HTML:

<li onclick="function_1();">
  <input type="checkbox" onclick="function_2();">
  <div onclick="function_3();">
</li>

My issue is: Whenever I click one of the child elements (either the checkbox or div), it also carries out the function from the <li> --> function_1() . 我的问题是:每当我单击子元素之一(复选框或div)时,它也会从<li> -> function_1()执行该功能。

How can I prevent the parent element function from being called when child element is being clicked? 如何防止单击子元素时调用父元素函数? What is the best way to achieve this? 实现此目标的最佳方法是什么?

Thanks for the help! 谢谢您的帮助!

使用stopPropagation函数

另外,您也可以从子元素onclick函数return false

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

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