如何在JavaScript中使用循环迭代几个元素?

huangapple go评论71阅读模式
英文:

How to iterate a few elements with loops in JavaScript?

问题

我正在制作一个常见问题解答部分。

目标非常简单:点击问题以展开答案,然后再次点击问题以关闭答案。与此同时,"+" 和 "-" 切换。

我已经让第一个问题可以工作了,但我不知道如何使用循环来迭代它。我尝试了 for 循环,但失败了。

有人可以帮忙吗?

let questionContainer1 = document.querySelector('#question-container-1');
let question1          = document.querySelector('#question-1');
let btn1               = document.querySelector('#btn-1');
let answer1            = document.querySelector('#answer-1');

let buttonSwitch = false;

function toggle() {
  answer1.classList.toggle('toggle-style');
  question1.classList.toggle('toggle-question-style');
  if (buttonSwitch === false) {
    console.log("false");
    btn1.innerHTML = "-";
    buttonSwitch = true;
  } else if (buttonSwitch === true) {
    console.log("true");
    btn1.innerHTML = '+';
    buttonSwitch = false;
  }
}

questionContainer1.addEventListener('click', toggle);
.FAQ-container {
  position: absolute;
  width: 60%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: solid 1px;
  display: flex;
  flex-direction: column;
  padding: 1rem;
}

h1 {
  margin: 0 auto;
  padding-bottom: 1rem;
  font-size: 20px;
  text-align: center;
}

.question-container {
  display: flex;
  justify-content: space between;
  font-size: 1em;
  cursor: pointer;
}

hr {
  width: 100%;
}

.answer {
  font-size: .9em;
  margin: 0;
  margin-bottom: 2rem;
  display: none;
  transition: all 1s;
}

.toggle-style {
  display: block;
}

.toggle-question-style {
  font-size: .8em;
}
<div class="FAQ-container">
  <h1>Frequently Asked Questions</h1>

  <div class="question-container" id="question-container-1">
    <div class="question" id="question-1">What is the return policy?</div>
    <div class="btn" id="btn-1">+</div>
  </div>
  <hr />
  <p class="answer" id="answer-1">
    Lorem ipsum, dolor sit amet consectetur adipisicing elit.
  </p>
</div>

(请注意,这只是第一个问题的示例代码,您需要根据需要重复这些代码块以处理其他问题。)

英文:

I'm making an FAQ section.

The goal is pretty simple: click the question to open the answer and then click the question again to close the answer.
At the meantime, the "+" and "-" toggles.

I made it work for the first question, but I don't knonw how to iterate it with loops.
I tried to for loop, but failed.

Can someone help?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let questionContainer1 = document.querySelector(&#39;#question-container-1&#39;);
let question1          = document.querySelector(&#39;#question-1&#39;);
let btn1               = document.querySelector(&#39;#btn-1&#39;);
let answer1            = document.querySelector(&#39;#answer-1&#39;);

let buttonSwitch = false;

function toggle() {
  answer1.classList.toggle(&#39;toggle-style&#39;);
  question1.classList.toggle(&#39;toggle-question-style&#39;);
  if (buttonSwitch === false) {
    console.log(&quot;false&quot;);
    btn1.innerHTML = &quot;-&quot;;
    buttonSwitch = true;
  } else if (buttonSwitch = true) {
    console.log(&quot;true&quot;);
    btn1.innerHTML = &#39;+&#39;;
    buttonSwitch = false;
  }
}

questionContainer1.addEventListener(&#39;click&#39;, toggle);

<!-- language: lang-css -->

.FAQ-container {
  position: absolute;
  width: 60%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: solid 1px;
  display: flex;
  flex-direction: column;
  padding: 1rem;
}

h1 {
  margin: 0 auto;
  padding-bottom: 1rem;
  font-size: 20px;
  text-align: center;
}

.question-container {
  display: flex;
  justify-content: space-between;
  font-size: 1em;
  cursor: pointer;
}

hr {
  width: 100%;
}

.answer {
  font-size: .9em;
  margin: 0;
  margin-bottom: 2rem;
  display: none;
  transition: all 1s;
}

.toggle-style {
  display: block;
}

.toggle-question-style {
  font-size: .8em;
}

<!-- language: lang-html -->

&lt;div class=&quot;FAQ-container&quot;&gt;
  &lt;h1&gt;Frequently Asked Questions&lt;/h1&gt;

  &lt;div class=&quot;question-container&quot; id=&quot;question-container-1&quot;&gt;
    &lt;div class=&quot;question&quot; id=&quot;question-1&quot;&gt;What is the return policy?&lt;/div&gt;
    &lt;div class=&quot;btn&quot; id=&quot;btn-1&quot;&gt;+&lt;/div&gt;
  &lt;/div&gt;
  &lt;hr /&gt;
  &lt;p class=&quot;answer&quot; id=&quot;answer-1&quot;&gt;
    Lorem ipsum, dolor sit amet consectetur adipisicing elit.
  &lt;/p&gt;

  &lt;div class=&quot;question-container&quot; id=&quot;question-container-2&quot;&gt;
    &lt;div class=&quot;question&quot;&gt;When will I receive my order?&lt;/div&gt;
    &lt;div class=&quot;btn&quot; id=&quot;btn-2&quot;&gt;+&lt;/div&gt;
  &lt;/div&gt;
  &lt;hr /&gt;
  &lt;p class=&quot;answer&quot; id=&quot;answer-2&quot;&gt;
    Lorem ipsum dolor sit amet consectetur, adipisicing elit.
  &lt;/p&gt;

  &lt;div class=&quot;question-container&quot; id=&quot;question-container-3&quot;&gt;
    &lt;div class=&quot;question&quot;&gt;Where are you located&lt;/div&gt;
    &lt;div class=&quot;btn&quot; id=&quot;btn-3&quot;&gt;+&lt;/div&gt;
  &lt;/div&gt;
  &lt;hr /&gt;

<!-- end snippet -->

答案1

得分: 0

为使您的FAQ部分具有动态性并遍历所有问题和答案,您可以使用循环向每个问题容器添加事件侦听器。

请按以下方式编写您的脚本:

// 获取所有问题容器
let questionContainers = document.querySelectorAll('.question-container');

// 遍历每个问题容器并添加一个事件侦听器以切换答案的显示
questionContainers.forEach((container) => {
  let question = container.querySelector('.question');
  let btn = container.querySelector('.btn');
  let answer = container.nextElementSibling;

  let buttonSwitch = false;
  function toggle(){
    answer.classList.toggle('toggle-style');
    question.classList.toggle('toggle-question-style');
    if(buttonSwitch === false){
      btn.innerHTML = "-";
      buttonSwitch = true;
    } else if (buttonSwitch === true) {
      btn.innerHTML = '+';
      buttonSwitch = false;
    }
  }

  container.addEventListener('click', toggle);
});

这段代码将通过循环遍历所有问题容器,并为每个容器添加一个点击事件侦听器,以切换答案的显示。

英文:

To make your FAQ section dynamic and iterate through all the questions and answers, you can use a loop to add event listeners to each question container.

Write your script as follows:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Get all the question containers
let questionContainers = document.querySelectorAll(&#39;.question-container&#39;);

// Loop through each question container and add an event listener to toggle the answer
questionContainers.forEach((container) =&gt; {
  let question = container.querySelector(&#39;.question&#39;);
  let btn = container.querySelector(&#39;.btn&#39;);
  let answer = container.nextElementSibling;

  let buttonSwitch = false;
  function toggle(){
    answer.classList.toggle(&#39;toggle-style&#39;);
    question.classList.toggle(&#39;toggle-question-style&#39;);
    if(buttonSwitch === false){
      
      btn.innerHTML = &quot;-&quot;;
      buttonSwitch = true;
    } else if (buttonSwitch === true) {
      
      btn.innerHTML = &#39;+&#39;;
      buttonSwitch = false;
    }
  }

  container.addEventListener(&#39;click&#39;, toggle);
});

<!-- end snippet -->

答案2

得分: 0

你不必创建那么多变量,但你可以查询所有元素并将相同的事件侦听器添加到每个元素上。

请注意,我将问题和答案包装在一个名为.question的容器中。

document.querySelectorAll('.question-container')
  .forEach((e) => e.addEventListener('click', toggle));

function toggle(event) {

  const questionContainer = event.currentTarget.parentElement;
  const question = event.currentTarget;
  const answer = questionContainer.getElementsByClassName('answer')[0];
  const btn = question.getElementsByClassName('btn')[0];
  
  answer.classList.toggle('toggle-style');
  question.classList.toggle('toggle-question-style');
  
  if (btn.innerHTML === '+') {
    btn.innerHTML = "-";
  } else {
    btn.innerHTML = '+';
  }
}
.FAQ-container {
  position: absolute;
  width: 60%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: solid 1px;
  display: flex;
  flex-direction: column;
  padding: 1rem;
}

h1 {
  margin: 0 auto;
  padding-bottom: 1rem;
  font-size: 20px;
  text-align: center;
}

.question-container {
  display: flex;
  justify-content: space between;
  font-size: 1em;
  cursor: pointer;
}

hr {
  width: 100%;
}

.answer {
  font-size: .9em;
  margin: 0;
  margin-bottom: 2rem;
  display: none;
  transition: all 1s;
}

.toggle-style {
  display: block;
}

.toggle-question-style {
  font-size: .8em;
}
<div class="FAQ-container">
  <h1>Frequently Asked Questions</h1>

  <div class="question">
    <div class="question-container" id="question-container-1">
      <div class="question" id="question-1">What is the return policy?</div>
      <div class="btn" id="btn-1">+</div>
    </div>
    <hr />
    <p class="answer" id="answer-1">
      Lorem ipsum, dolor sit amet consectetur adipisicing elit.
    </p>
  </div>
  <div class="question">
    <div class="question-container" id="question-container-2">
      <div class="question">When will I receive my order?</div>
      <div class="btn" id="btn-2">+</div>
    </div>
    <hr />
    <p class="answer" id="answer-2">
      Lorem ipsum dolor sit amet consectetur, adipisicing elit.
    </p>
  </div>
  <div class="question">
    <div class="question-container" id="question-container-3">
      <div class="question">Where are you located</div>
      <div class="btn" id="btn-3">+</div>
    </div>
    <hr />
    <p class="answer" id="answer-3">
      Lorem ipsum dolor sit amet consectetur, adipisicing elit.
    </p>
  </div>
</div>
英文:

You don't have to create so many variables but you can query all the elements and add the same event listener to each of them.

Please note that I wrapped the questions and answers in a container called .question

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelectorAll(&#39;.question-container&#39;)
  .forEach((e) =&gt; e.addEventListener(&#39;click&#39;, toggle));

function toggle(event) {

  const questionContainer = event.currentTarget.parentElement;
  const question = event.currentTarget;
  const answer = questionContainer.getElementsByClassName(&#39;answer&#39;)[0];
  const btn = question.getElementsByClassName(&#39;btn&#39;)[0];
  
  answer.classList.toggle(&#39;toggle-style&#39;);
  question.classList.toggle(&#39;toggle-question-style&#39;);
  
  if (btn.innerHTML === &#39;+&#39;) {
    btn.innerHTML = &quot;-&quot;;
  } else {
    btn.innerHTML = &#39;+&#39;;
  }
}

<!-- language: lang-css -->

.FAQ-container {
  position: absolute;
  width: 60%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: solid 1px;
  display: flex;
  flex-direction: column;
  padding: 1rem;
}

h1 {
  margin: 0 auto;
  padding-bottom: 1rem;
  font-size: 20px;
  text-align: center;
}

.question-container {
  display: flex;
  justify-content: space-between;
  font-size: 1em;
  cursor: pointer;
}

hr {
  width: 100%;
}

.answer {
  font-size: .9em;
  margin: 0;
  margin-bottom: 2rem;
  display: none;
  transition: all 1s;
}

.toggle-style {
  display: block;
}

.toggle-question-style {
  font-size: .8em;
}

<!-- language: lang-html -->

&lt;div class=&quot;FAQ-container&quot;&gt;
  &lt;h1&gt;Frequently Asked Questions&lt;/h1&gt;

  &lt;div class=&quot;question&quot;&gt;
    &lt;div class=&quot;question-container&quot; id=&quot;question-container-1&quot;&gt;
      &lt;div class=&quot;question&quot; id=&quot;question-1&quot;&gt;What is the return policy?&lt;/div&gt;
      &lt;div class=&quot;btn&quot; id=&quot;btn-1&quot;&gt;+&lt;/div&gt;
    &lt;/div&gt;
    &lt;hr /&gt;
    &lt;p class=&quot;answer&quot; id=&quot;answer-1&quot;&gt;
      Lorem ipsum, dolor sit amet consectetur adipisicing elit.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;question&quot;&gt;
    &lt;div class=&quot;question-container&quot; id=&quot;question-container-2&quot;&gt;
      &lt;div class=&quot;question&quot;&gt;When will I receive my order?&lt;/div&gt;
      &lt;div class=&quot;btn&quot; id=&quot;btn-2&quot;&gt;+&lt;/div&gt;
    &lt;/div&gt;
    &lt;hr /&gt;
    &lt;p class=&quot;answer&quot; id=&quot;answer-2&quot;&gt;
      Lorem ipsum dolor sit amet consectetur, adipisicing elit.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;question&quot;&gt;
    &lt;div class=&quot;question-container&quot; id=&quot;question-container-3&quot;&gt;
      &lt;div class=&quot;question&quot;&gt;Where are you located&lt;/div&gt;
      &lt;div class=&quot;btn&quot; id=&quot;btn-3&quot;&gt;+&lt;/div&gt;
    &lt;/div&gt;
    &lt;hr /&gt;
    &lt;p class=&quot;answer&quot; id=&quot;answer-3&quot;&gt;
      Lorem ipsum dolor sit amet consectetur, adipisicing elit.
    &lt;/p&gt;
  &lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。

以下是要翻译的内容:

"You are cluttering up with way too many lines of code..."

  • 你的代码行数太多,有点混乱...

"This is a typical use case for event delegation (widely explained on StackOverflow), which allows you to add as many questions/answers, without worrying about assigning unnecessary IDs to them."

  • 这是事件委托的典型用例(在StackOverflow上有广泛解释),它允许你添加尽可能多的问题/答案,而无需担心为它们分配不必要的ID。

"I also added a feature to keep only one question/answer open at a time."

  • 我还添加了一个功能,每次只保持一个问题/答案处于展开状态。
英文:

You are cluttering up with way too many lines of code...

This is a typical use case for event delegation (widely explained on StackOverflow), whicth allow you to add as many questions/answers, without worrying about assigning unnecessary IDs to them.

I also added a feature to keep only one question/answer open at a time.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
  d_FAQ_container = document.querySelector(&#39;div.FAQ-container&#39;)
, All_questions   = d_FAQ_container.querySelectorAll(&#39;.question-container&#39;)
  ;              //   ^--- only it childs
  
d_FAQ_container.addEventListener(&#39;click&#39;, f_toggle );

function f_toggle ( evt )
  {
  let elmRef = evt.target.closest(&#39;.question-container&#39;) || d_FAQ_container;
             //           ^--- check 4 this parent -^
             
  if (!elmRef.matches(&#39;.question-container&#39;) ) return; // exit for others...

  if (elmRef.classList.toggle(&#39;toggle-question-style&#39;) ) // this return a Boolean
    {
    // keep only one question/answer open at a time...
    All_questions.forEach( qElm =&gt;
      qElm.classList.toggle(&#39;toggle-question-style&#39;, qElm === elmRef ));
    }                                              //      ^-- force
  }

<!-- language: lang-css -->

.FAQ-container {
  position       : absolute;
  width          : 60%;
  top            : 50%;
  left           : 50%;
  transform      : translate(-50%, -50%);
  border         : solid 1px;
  display        : flex;
  flex-direction : column;
  padding        : 1rem;
  }
h1 {
  margin         : 0 auto;
  padding-bottom : 1rem;
  font-size      : 20px;
  text-align     : center;
  }
div.question-container {
  display         : flex;
  justify-content : space-between;
  font-size       : 1em;
  cursor          : pointer;
  }
div.question-container.toggle-question-style {
  font-size : .8em;
  }
div.question-container &gt; div.btn::before {
  content : &#39;+&#39;;
  }
div.question-container.toggle-question-style &gt; div.btn::before {
  content : &#39;-&#39;;
  }
hr {
  width : 100%;
  }
p.answer {
  font-size      : .9em;
  margin         : 0;
  margin-bottom  : 2rem;
  transition     : all 1s;
  }
div.question-container:not(.toggle-question-style) + hr + p.answer {
  display : none;
  }

<!-- language: lang-html -->

&lt;div class=&quot;FAQ-container&quot;&gt;
    &lt;h1&gt;Frequently Asked Questions&lt;/h1&gt;

    &lt;div class=&quot;question-container&quot; &gt; &lt;!-- no needs to use ID --&gt;
      &lt;div class=&quot;question&quot;&gt;What is the return policy?&lt;/div&gt;
      &lt;div class=&quot;btn&quot;&gt;&lt;/div&gt;  &lt;!-- use css ::before to add &#39;+&#39; or &#39;-&#39; --&gt;
    &lt;/div&gt;
    &lt;hr&gt;
    &lt;p class=&quot;answer&quot;&gt;  &lt;!-- no needs to use ID --&gt;
      Lorem ipsum, dolor sit amet consectetur adipisicing elit.
    &lt;/p&gt;
  
    &lt;div class=&quot;question-container&quot; &gt;
      &lt;div class=&quot;question&quot;&gt;When will I receive my order?&lt;/div&gt;
      &lt;div class=&quot;btn&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;hr&gt;
    &lt;p class=&quot;answer&quot;&gt;
      Lorem ipsum dolor sit amet consectetur, adipisicing elit.
    &lt;/p&gt;
  
    &lt;div class=&quot;question-container&quot; &gt;
      &lt;div class=&quot;question&quot;&gt;Where are you located&lt;/div&gt;
      &lt;div class=&quot;btn&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;hr&gt;
    &lt;p class=&quot;answer&quot; &gt;
      Lorem ipsum dolor sit amet consectetur, adipisicing elit.
    &lt;/p&gt;
  &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月3日 22:53:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定