简体   繁体   English

如何使用单击事件侦听器制作按钮切换显示?

[英]How to make a button toggle display with click event listener?

I have about 20 products, each product has a button and description box.我有大约 20 个产品,每个产品都有一个按钮和描述框。 I have looped through added a click event listener that displays the product description box once the button is clicked.我已经循环添加了一个单击事件侦听器,一旦单击按钮就会显示产品描述框。

How can I give the button the ability to hide the description box as well (ie toggle display)?我怎样才能让按钮也能够隐藏描述框(即切换显示)?

Here is my code:这是我的代码:

const descriptionBtn = document.querySelectorAll('.desc-btn');

descriptionBtn.forEach((btn) => {
  btn.addEventListener('click', (e) => {
    descriptionBox = btn.nextElementSibling;
    descriptionBox.style.display = "block";
  });
});

You can use the button click handler to toggle a class on the description text to toggle its visibility.您可以使用按钮单击处理程序来切换描述文本上的 class 以切换其可见性。 Full working example:完整的工作示例:

 const descBtns = document.querySelectorAll('.desc-btn'); descBtns.forEach((btn) => { btn.addEventListener('click', (e) => { descText = btn.nextElementSibling; descText.classList.toggle('show'); }); });
 .desc-btn { display: block; margin-bottom: 10px; }.desc { display: none; }.desc.show { display: block; }
 <button class="desc-btn">desc btn</button> <p class="desc">description here</p> <button class="desc-btn">desc btn 2</button> <p class="desc">description here 2</p> <button class="desc-btn">desc btn 3</button> <p class="desc">description here 3</p>

You can simply toggle a class on the box.您可以简单地切换盒子上的 class。 Create a css class that contains: display:block.创建一个包含:display:block 的 css class。 Then toggle that class on the classList of the box.然后在框的 classList 上切换 class 。

.show{display:block;}

const descriptionBtn = document.querySelectorAll('.desc-btn');

descriptionBtn.forEach((btn)=>{
  btn.addEventListener('click', (e)=>{
    descriptionBox = btn.nextElementSibling;
    descriptionBox.classList.toggle("show");
  });
});
const descriptionBtn = document.querySelectorAll('.desc-btn');

descriptionBtn.forEach(element => {
  element.onclick = function (e) {
    if (this.style.display === "block") {
      this.style.display = "none";
    } else {
      this.style.display = "block";
    }
  };
});

You can use the tertiary operator to ask if the box is currently being displayed.您可以使用三级运算符来询问该框当前是否正在显示。 If it is the block will be set to hidden, if it is not then it will be set to display block.如果是块将设置为隐藏,如果不是则将其设置为显示块。 This is a common design pattern in other JS frameworks so it might be worth trying it out.这是其他 JS 框架中常见的设计模式,因此值得一试。

const descriptionBtn = document.querySelectorAll('.desc-btn');

descriptionBtn.forEach((btn) => {
  btn.addEventListener('click', (e) => {
    descriptionBox = btn.nextElementSibling;
    descriptionBox.style.display === "block" ? discriptionBox.style.display = 'hidden' :
      discriptionBox.style.display = 'block'
  });
});

Add two event listeners for the button with class "hide-background", "show-background".使用 class "hide-background", "show-background" 为按钮添加两个事件侦听器。 When any is clicked, change element class to the other.单击任何一个时,将元素 class 更改为另一个。

.show-background div{ display:block};
.hide-background div{ display:none};

const showBgnd = document.getElementsByClassName('show-background');
const hideBgnd = document.getElementsByClassName('hide-background');

showBgnd.addEventListener('click', (e)=>{
  showBgnd.setAttribute("class","hide-background");
});

hideBgnd.addEventListener('click', (e)=>{
  hideBgnd.setAttribute("class","show-background");
});

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

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