Javascript/jQuery 单击元素内的元素

发布于 2024-09-12 20:27:50 字数 405 浏览 3 评论 0原文

这可能是容易解决的问题,但由于某种原因,我无法弄清楚。

我有以下 HTML:

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

我的问题是:每当我单击其中一个子元素(复选框或 div)时,它也会执行

  • --> 中的功能。 function_1()
  • 如何防止单击子元素时调用父元素函数?实现这一目标的最佳方法是什么?

    感谢您的帮助!

    This is probably something quite easy to resolve, but for some reason, I can't figure it out.

    I have the following 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().

    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!

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    疧_╮線 2024-09-19 20:27:50

    使用 stopPropagation 函数

    Use the stopPropagation function

    秉烛思 2024-09-19 20:27:50

    或者,您可以从子元素 onclick 函数中返回 false

    Alternatively you can just return false from the child elements onclick function.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文