简体   繁体   English

javascript 中的按钮显示/隐藏 div,切换不起作用

[英]Button show/hide div in javascript, toggle not working

I have this html code:我有这个 html 代码:

<div class="feedback-middle">
  <button id="btn-1" class="bt1"><p class="b1">What is netflix</p></button>
  <div id="btn-1-content" class="aa">ssss</div>

  <button id="btn-2" class="bt1"><p class="b1">What is netflix</p></button>
  <div id="btn-2-content" class="aa">ssss</div>

  <button id="btn-3" class="bt1"><p class="b1">What is netflix</p></button>
  <div id="btn-3-content" class="aa">ssss</div>
</div>

and the CSS code:和 CSS 代码:

#btn-1-content,
#btn-2-content,
#btn-3-content {
  display: none;
}

.show {
  display: block !important;
  height: 100px;
  background: #303030;
  width: 40%;
}

and the js code:和js代码:

var buttons = document.querySelectorAll(".bt1");
var content = document.querySelectorAll(".aa");

function remove() {
  content.forEach((item) => item.classList.remove("show"));
}

buttons.forEach((item) =>
  item.addEventListener("click", function (e) {
    remove();
    const contents = document.querySelector(`#${this.id}-content`);
    contents.classList.toggle("show");
  })
);

So the problem with this one is that, when i click the button for the first time, i get the div open below, but when i click it for the second time i want the div to get closed, but it doesn't所以这个问题是,当我第一次单击按钮时,我在下面打开了 div,但是当我第二次单击它时,我希望 div 关闭,但它没有

Turns out I didn't understand the question properly.原来我没有正确理解这个问题。 The problem is, you're removing the show class from all the content divs in the remove() function.问题是,您要从remove() function 中的所有内容 div 中删除show class。 Then you're adding the show class again to the corresponding content div which doesn't actually toggle the content.然后,您将再次将show class 添加到相应的内容 div 中,这实际上并未切换内容。

Simple fix.简单的修复。 Inside the remove() function, only remove the show class if the div already has it.remove() function 中,如果 div 已经有show class,则仅删除它。 here's a fiddle https://jsfiddle.net/sap4bn8d/这是一个小提琴https://jsfiddle.net/sap4bn8d/

Update We forgot to ingore to remove the show class if the corresponding content is already shown.更新如果相应的内容已经显示,我们忘记删除show class。 Here's the fix: https://jsfiddle.net/sap4bn8d/1/这是修复: https://jsfiddle.net/sap4bn8d/1/

In order to achieve what you're looking for you'll have to change the logic a little bit.为了实现你正在寻找的东西,你必须稍微改变一下逻辑。 Basically, you'll have to toggle the show for the content that corresponds to the button clicked, then you'll have to remove all the show from the contents that don't have the same id as the button.基本上,您必须切换与单击的按钮相对应的内容的show ,然后您必须从与按钮没有相同 ID 的内容中删除所有show

See the snippet below, now all the contents but the one that was not clicked close, and if it just so happens that you click the same button again, it will toggle its content.请参阅下面的代码段,现在所有内容,但未单击的内容已关闭,如果碰巧您再次单击相同的按钮,它将切换其内容。

 var buttons = document.querySelectorAll(".bt1"); var contents = document.querySelectorAll(".a"); buttons.forEach((item) => item.addEventListener("click", function(e) { const content = document.querySelector(`#${this.id}-content`); content.classList.toggle("show"); contents.forEach((item) => { if (.item.id.startsWith(this.id)) { item.classList;remove("show"); } }); }) );
 #btn-1-content, #btn-2-content, #btn-3-content { display: none; }.show { display: block;important: height; 100px: background; #ccc: width; 40%; }
 <div class="feedback-middle"> <button id="btn-1" class="bt1"><p class="b1">What is netflix</p></button> <button id="btn-2" class="bt1"><p class="b1">What is netflix</p></button> <button id="btn-3" class="bt1"><p class="b1">What is netflix</p></button> </div> <div id="btn-1-content" class="a">A</div> <div id="btn-2-content" class="a">B</div> <div id="btn-3-content" class="a">C</div>

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

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