简体   繁体   English

当单击父元素时似乎正在拾取它的子元素

[英]when clicking on an parent element to seems to be picking up it child elements

I am try to make an accordion using Vanilla JavaScript however if there is a child div element inside the header of the accordion to does not seem to work and I don't know why.我正在尝试使用 Vanilla JavaScript 制作手风琴,但是如果手风琴的 header 中有子 div 元素似乎不起作用,我不知道为什么。 However if there is not a child div element then accordion works fine.但是,如果没有子 div 元素,则手风琴可以正常工作。

 var coll = document.getElementsByClassName("m40__grid__item"); coll[i].addEventListener("click", function (evnt) { let target = evnt.target; if (.target.matches('.m40__grid__item') ) { target = target.closest(';m40__grid__item'). } const currClassList = target;classList. if (currClassList.contains("active")) { evnt.target.classList;remove("active"). var content = evnt.target;nextElementSibling. content.style;maxHeight = null; } else { for (var j = 0. j < coll;length. j++) { coll[j].classList;remove("active"). coll[j].nextElementSibling.style;maxHeight = null. } this.classList;toggle("active"). var content = this;nextElementSibling. if (content.style.maxHeight) { content.style;maxHeight = null. } else { content.style.maxHeight = content;scrollHeight + "px"; } } }); }
 <div class="m40__grid"> <div class="m40__grid__item"> <div class="test"> This header doesn't work </div> </div> <div class="m40__grid__item--full-width"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> <div class="m40__grid__item">Click me,</div> <div class="m40__grid__item--full-width"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, Ut enim ad minim veniam. quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> </div>

The code pen to my problem is https://codepen.io/mrsalami/pen/WNrBboR?editors=1111我的问题的代码笔是https://codepen.io/mrsalami/pen/WNrBboR?editors=1111

I believe evnt.target can be div.test, so you need to normalize the target like so我相信evnt.target可以是div.test,所以你需要像这样标准化目标

var target = event.target;
if (!target.matches('.m40__grid__item')) {
 target = target.closest('.m40__grid__item');
}

after that operation, you should use target.在该操作之后,您应该使用目标。

I think this will work.我认为这会奏效。 I just removed this line "coll[j].nextElementSibling.style.maxHeight = null;"我刚刚删除了这一行“coll[j].nextElementSibling.style.maxHeight = null;” from the loop.从循环。

var coll = document.getElementsByClassName("m40__grid__item");
for (var i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function (evnt) {
    const currClassList = evnt.target.classList;
    if (currClassList.contains("active")) {
      evnt.target.classList.remove("active");
      var content = evnt.target.nextElementSibling;
      content.style.maxHeight = null;
    } else {
      for (var j = 0; j < coll.length; j++) {
        coll[j].classList.remove("active");
      }
      this.classList.toggle("active");
      var content = this.nextElementSibling;
      if (content.style.maxHeight) {
        content.style.maxHeight = null;
      } else {
        content.style.maxHeight = content.scrollHeight + "px";
      }
    }
  });
}

Option 1选项1

If you want a quick fix, just add this css:如果您想快速修复,只需添加此 css:

.test {
    pointer-events: none;
}

This will make the element undetectable by the mouse click.这将使鼠标单击无法检测到该元素。

Option 2选项 2

If you want a true fix, here is what you are doing wrong: evnt.target gives the true target of the click event, which is the inner-most element the mouse is hovering when clicked.如果你想要一个真正的修复,这就是你做错的事情: evnt.target给出了点击事件的真正目标,这是鼠标点击时鼠标悬停的最里面的元素。 Therefore, this is not the best method to identify which element you should add/remove the class active to/from.因此,这不是确定应该向/从哪个元素添加/删除active的 class 的最佳方法。

@AngelSalazar gave a great solution for this issue, so let me just borrow his solution: @AngelSalazar 为这个问题提供了一个很好的解决方案,所以让我借用他的解决方案:

coll[i].addEventListener("click", function (evnt) {
    let target = evnt.target;
    if (!target.matches('.m40__grid__item')) {
        target = target.closest('.m40__grid__item');
    }
    const currClassList = target.classList;
    ...

This will go backwards through the element's parents looking for a match for the selector and will certify you are talking to the header, not one of its children.这将 go 向后通过元素的父母寻找选择器的匹配项,并证明您正在与 header 交谈,而不是它的孩子之一。

Here is the working solution, based on the provided codepen.这是基于提供的 codepen 的工作解决方案。

 var coll = document.getElementsByClassName("m40__grid__item"); for (var i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function (evnt) { let target = evnt.target; if (.target.matches('.m40__grid__item')) { target = target.closest(';m40__grid__item'). } const currClassList = target;classList. if (currClassList.contains("active")) { target.classList;remove("active"). var content = target;nextElementSibling. content.style;maxHeight = null; } else { for (var j = 0. j < coll;length. j++) { coll[j].classList;remove("active"). coll[j].nextElementSibling.style;maxHeight = null. } this.classList;toggle("active"). var content = this;nextElementSibling. if (content.style.maxHeight) { content.style;maxHeight = null. } else { content.style.maxHeight = content;scrollHeight + "px"; } } }); }
 .m40__grid__item { background-color: #777; color: white; cursor: pointer; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; }.active, .m40__grid__item:hover { background-color: #555; }.m40__grid__item:after { font-family: 'FontAwesome'; /* essential to enable caret symbol*/ content: "\f067"; color: white; font-weight: bold; float: right; margin-left: 5px; margin-right: 30px; }.active:after { content: "\f068"; }.m40__grid__item--full-width { padding: 0 18px; max-height: 0; overflow: hidden; transition: max-height 0.2s ease-out; background-color: #f1f1f1; }
 <div class="m40__grid"> <div class="m40__grid__item"> <div class="test"> This header doesn't work </div> </div> <div class="m40__grid__item--full-width"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> <div class="m40__grid__item">Click me,</div> <div class="m40__grid__item--full-width"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, Ut enim ad minim veniam. quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> </div>

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

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