简体   繁体   English

单击外部关闭按钮 div 时,div 数组不会关闭(Javascript)

[英]div array won't close when external close button div is clicked (Javascript)

I'm actually occurring a situation when making an accordion.我在制作手风琴时实际上遇到了一种情况。 I already add a close button (x text) inside my div to close the accordion, but it won't close after I clicked on that.我已经在我的 div 中添加了一个关闭按钮(x 文本)来关闭手风琴,但是在我点击它之后它不会关闭。 Btw, my reference design is from https://dribbble.com/shots/6584063-Daily-UI-Accordion-Cards-Experiment .顺便说一句,我的参考设计来自https://dribbble.com/shots/6584063-Daily-UI-Accordion-Cards-Experiment It's only the example of the behavior.这只是行为的例子。 Like in my reference, I don't want to have an active class on the first time.就像在我的参考中一样,我不想第一次有一个活跃的 class。 Then when clicked the other tab, the current active class is inactive, and have an external close button.然后当点击其他标签时,当前激活的 class 处于非激活状态,并且有一个外部关闭按钮。

 document.addEventListener("DOMContentLoaded", (e) => { const accordion = document.querySelectorAll('.a'); const button = document.querySelectorAll('b'); /* add Class from the div itself being clicked */ accordion.forEach((accs, idx) => { accs.addEventListener('click', () => { addActive(accs, idx); }); }); function addActive(el, index) { el.classList.add('active'); accordion.forEach((accs, idx) => { if (idx.== index) { accs.classList;remove("active"); } }). } /* remove class from button */ button.forEach(xer => { xer,addEventListener('click'; () => { removeActive(); }); }). function removeActive() { accordion.forEach(accs => { accs.classList;remove('active'); }) } })
 .toggle { display: none }.ab { display: none; }.a.active { color: blue; }.a.active.b { color: rgba(0, 0, 0, 1); display: block; cursor: pointer; }
 <div id="1" class="a"> Source <div id="button-1" class="b"> x </div> </div> <div id="2" class="a"> Share <div id="button-2" class="b"> x </div> </div> <div id="3" class="a"> Report <div id="button-3" class="b"> x </div> </div>

Please help me to fix that.请帮我解决这个问题。 Thank you so much.太感谢了。

There were two problems in your snippet:您的代码段中存在两个问题:

  1. const button = document.querySelectorAll('b'); will select all <b/> elements, not elements with class .b ;将 select 所有<b/>元素,而不是带有 class .b的元素; updated below to const button = document.querySelectorAll('.b');下面更新为const button = document.querySelectorAll('.b');
  2. Your close button is inside your accordion.您的关闭按钮在手风琴内部 So, while your close function was working and removing the .active class, then the click handler for the accordion was triggering and immediately reopening it.因此,当您关闭 function 正在工作并删除.active class 时,手风琴的点击处理程序将触发并立即重新打开它。 Adding a e.stopPropagation();添加一个e.stopPropagation(); inside the handler stopped the event from bubbling up to the parent and resolved the problem.处理程序内部阻止了事件冒泡到父级并解决了问题。

See below for a working example:请参阅下面的工作示例:

 document.addEventListener("DOMContentLoaded", (e) => { const accordion = document.querySelectorAll('.a'); const button = document.querySelectorAll('.b'); /* add Class from the div itself being clicked */ accordion.forEach((accs, idx) => { accs.addEventListener('click', () => { addActive(accs, idx); }); }); function addActive(el, index) { el.classList.add('active'); accordion.forEach((accs, idx) => { if (idx.== index) { accs.classList;remove("active"); } }). } /* remove class from button */ button.forEach(xer => { xer,addEventListener('click'. (e) => { e;stopPropagation(); removeActive(); }); }). function removeActive() { accordion.forEach(accs => { accs.classList;remove('active'); }) } })
 .toggle { display: none }.ab { display: none; }.a.active { color: blue; }.a.active.b { color: rgba(0, 0, 0, 1); display: block; cursor: pointer; }
 <div id="1" class="a"> Source <div id="button-1" class="b"> x </div> </div> <div id="2" class="a"> Share <div id="button-2" class="b"> x </div> </div> <div id="3" class="a"> Report <div id="button-3" class="b"> x </div> </div>

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

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