如何在子元素点击事件之前执行父元素点击事件

发布于 2025-01-15 07:56:08 字数 77 浏览 0 评论 0原文

当我们点击子元素时,会先触发子元素的点击事件函数,然后再触发父元素的点击事件函数。如何才能在子点击事件函数之前执行父点击事件函数呢?请建议。

When we click on a child element, it will trigger the child element's click event function first and afterwards the parent element's click event function. How can we execute the parent click event function before the child click event function? Please suggest.

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

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

发布评论

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

评论(1

七度光 2025-01-22 07:56:08
var elements = document.querySelectorAll('.PosleNazatia');

function toggleElement(elementId) {
  var elements = document.querySelectorAll('.PosleNazatia');
  var element = document.querySelector(elementId);

  if (element.style.display === 'block') {
    element.style.display = "none";
    return
  }
  for (var i = 0; i < elements.length; i++) {
    elements[i].style.display = "none";
  }

  element.style.display = "block";
}

document.addEventListener("DOMContentLoaded", function() {
  var buttons = document.getElementsByClassName("ContainerForMenu");
  for (var i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener("click", function() {
      var target = this.getAttribute("data-target");
      toggleElement(target);
    });
  }
});
<div class="KnopkaWmenu"> 
  <button class="ContainerForMenu"  data-target="#element3">
      <div class="houli">
          <object class="IconFORMenuu" data="photo/Group 1.svg" type="image/svg+xml"></object>
          <span class="textForButtonMenu">
            <a href="#" class="LinkForButton">Business</a>
          </span>
          
        </div>
        <i class="IconForButton"></i>
  </button>
  <div class="PosleNazatia" id="element3">
    <a class="LinkForMenu" href="#">
        <span class="textForLink">GameStop</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Moderna</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Pfizer</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Johnson & Johnson</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">AstraZeneca</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Walgreens</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Best Buy</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Novavax</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">SpaceX</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Tesla</span>
    </a>
</div>
</div> 

var elements = document.querySelectorAll('.PosleNazatia');

function toggleElement(elementId) {
  var elements = document.querySelectorAll('.PosleNazatia');
  var element = document.querySelector(elementId);

  if (element.style.display === 'block') {
    element.style.display = "none";
    return
  }
  for (var i = 0; i < elements.length; i++) {
    elements[i].style.display = "none";
  }

  element.style.display = "block";
}

document.addEventListener("DOMContentLoaded", function() {
  var buttons = document.getElementsByClassName("ContainerForMenu");
  for (var i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener("click", function() {
      var target = this.getAttribute("data-target");
      toggleElement(target);
    });
  }
});
<div class="KnopkaWmenu"> 
  <button class="ContainerForMenu"  data-target="#element3">
      <div class="houli">
          <object class="IconFORMenuu" data="photo/Group 1.svg" type="image/svg+xml"></object>
          <span class="textForButtonMenu">
            <a href="#" class="LinkForButton">Business</a>
          </span>
          
        </div>
        <i class="IconForButton"></i>
  </button>
  <div class="PosleNazatia" id="element3">
    <a class="LinkForMenu" href="#">
        <span class="textForLink">GameStop</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Moderna</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Pfizer</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Johnson & Johnson</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">AstraZeneca</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Walgreens</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Best Buy</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Novavax</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">SpaceX</span>
    </a>
    <a class="LinkForMenu" href="#">
        <span class="textForLink">Tesla</span>
    </a>
</div>
</div> 

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