简体   繁体   English

当我只单击其中一个问题时,我需要帮助禁用其他问题的下拉样式

[英]I need HELP in disabling dropdown styles for other questions when I click on just one of them

I brought the necessary dom elements in with querySelectorAll(), I have been able to enable and disable the styles I want for each individual FAQ question dropdown when I Click but I also want to be able to disable the dropdowns for any other open questions that might be open when I click on just one.我使用 querySelectorAll() 引入了必要的 dom 元素,当我单击时,我已经能够为每个单独的常见问题解答问题下拉列表启用和禁用我想要的样式,但我也希望能够禁用任何其他未解决问题的下拉列表当我只点击一个时可能会打开。

JS script JS脚本

const questionContainer = document.querySelectorAll('.question');

function triggerDropdown() {
  questionContainer.forEach((q) => {
    const summary = q.querySelector('.summary'),
      img = q.querySelector('.img'),
      dropdown = q.querySelector('.dropdown');


    q.addEventListener('click', function () {
      img.classList.toggle('flip');
      summary.classList.toggle('bold');
      
      summary.classList.contains('bold')
        ? (dropdown.style.display = 'inline-block')
        : (dropdown.style.display = 'none');
    });
  });
}

CSS styles to be changed要更改的 CSS 样式

.dropdown {
  display: none;
}

/* Activate with JS */
.bold {
  font-weight: 700;
  font-size: 13px;
}

.flip {
  transform: scaleY(-1);
}

HTML HTML

      <div class="content">
        <h1>FAQ</h1>
        <br />
<div class="question">
        <p class="summary">QUESTION ASKED?<span class="arrow"><img class="img" src="images/icon-arrow-down.svg" alt="dropdown arrow"></span></p>
        <p class="dropdown">
 ANSWERS TO QUESTION
        </p>
      </div>
<div class="question">
        <p class="summary">QUESTION ASKED?<span class="arrow"><img class="img" src="images/icon-arrow-down.svg" alt="dropdown arrow"></span></p>
        <p class="dropdown">
        ANSWERS TO QUESTION
        </p>
      </div>
<div class="question">
        <p class="summary">QUESTION ASKED?<span class="arrow"><img class="img" src="images/icon-arrow-down.svg" alt="dropdown arrow"></span></p>
        <p class="dropdown">
 ANSWERS TO QUESTION
        </p>
      </div>
<div class="question">
        <p class="summary">QUESTION ASKED?<span class="arrow"><img class="img" src="images/icon-arrow-down.svg" alt="dropdown arrow"></span></p>
        <p class="dropdown">
           ANSWERS TO QUESTION
        </p>
      </div>
<div class="question">
        <p class="summary">QUESTION ASKED?<span class="arrow"><img class="img" src="images/icon-arrow-down.svg" alt="dropdown arrow"></span></h1>
        <p class="dropdown">
          ANSWERS TO QUESTION
        </p>
      </div>

      </div>

Essentially you could iterate over all the dropdowns:基本上你可以遍历所有的下拉菜单:

const questionContainer = document.querySelectorAll('.question');
const dropdowns = document.querySelectorAll('.dropdown');

  questionContainer.forEach((q) => {
    const summary = q.querySelector('.summary'),
      img = q.querySelector('.img');

    q.addEventListener('click', function (event) {    
      img.classList.toggle('flip');
      summary.classList.toggle('bold');

      dropdowns.forEach (dropdown => {
          if (dropdown !== q.querySelector('.dropdown')) {
              // the dropdown that was clicked

              summary.classList.contains('bold')
                ? (dropdown.style.display = 'inline-block')
                : (dropdown.style.display = 'none');
          }
          else {
              // one of the other non-clicked dropdowns


          }
      });
    });
  });

With Bryan Grace's suggestion and help, I was able to figure out a way to toggle the css properties for the dropdown and at the same time remove those same properties for the unclicked questions.在 Bryan Grace 的建议和帮助下,我找到了一种方法来切换下拉列表的 css 属性,同时删除未点击问题的相同属性。

Basically I iterated over the dropdowns, arrows, and summaries with a forEach to see if it matched with the clicked item, if it did not, the css properties were removed.基本上,我使用 forEach 遍历下拉菜单、箭头和摘要,以查看它是否与单击的项目匹配,如果不匹配,则删除 css 属性。

const questionContainer = document.querySelectorAll('.question');
const dropdowns = document.querySelectorAll('.dropdown');
const summaries = document.querySelectorAll('.summary');
const arrows = document.querySelectorAll('.img');

questionContainer.forEach((q) => {
  // individual variables for each question container
  const summary = q.querySelector('.summary'),
    qArrow = q.querySelector('.img');

  // Event Listener
  q.addEventListener('click', function () {
    qArrow.classList.toggle('flip');
    summary.classList.toggle('bold');

    dropdowns.forEach((dropdown) => {
      // If dropdown clicked matches dropdown iterated over enable dropdown properties
      if (dropdown === q.querySelector('.dropdown')) {
        summary.classList.contains('bold')
          ? (dropdown.style.display = 'inline-block')
          : (dropdown.style.display = 'none');

        //   If not a match, remove dropdown properties
      } else if (dropdown !== q.querySelector('.dropdown')) {
        dropdown.style.display = 'none';
        summaries.forEach((s) => {
          if (s !== summary) {
            s.classList.remove('bold');
          }
        });
        arrows.forEach((arrow) => {
          if (arrow !== qArrow) {
            arrow.classList.remove('flip');
          }
        });
      }
    });
  });
});

Although I feel like the code for this operation should be a lot less.虽然我觉得这个操作的代码应该少很多。

暂无
暂无

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

相关问题 当我点击编辑按钮时出现问题,所有其他元素都听点击,但我只需要我点击的那个 - A problem when i click on edit button, all the other elements listen to the click but i need just the one i click on 当我点击其中一个元素时,所有元素都会消失,但只需要我没有点击的元素消失 - All the elements disappear when I click on one of them, but only need the elements that I haven't clicked on to disappear 当单击一个下拉菜单时,其他下拉菜单将关闭 - when click on the one dropdown other dropdown will be close 我在我的计算器键上添加了点击效果,但是当你点击其中一个时,其他的也会移动?! 你能帮助我吗? - I added a click effect to my calculator keys but when you click one of them the others move too!! Can you help me? javascript中的动画功能,当我单击它时需要对某物进行动画处理,以便一个div进入1种方式,另一个div进入另一种方式 - Animate function in javascript, I need to animate something when I click on it so that one div goes 1 way and the other div goes the other way 我需要帮助来简化此下拉功能 - I need help simplifying this dropdown function 在使用Bootstrap 4扩展另一个时,我需要折叠一个帮助 - I need help collapsing one when expanding another using bootstrap 4 单击其中之一时如何切换两个图像? - how to toggle two image when I click one of them? 单击外部下拉菜单时关闭下拉菜单 - Close Dropdown when I click outside dropdown 单击下拉列表时,我需要填充一个表格。 哪个选项表现更好? - I need to populate a form when I click on a dropdown. Which option is more performant?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM