我不知道该如何逻辑地写那个问题,我几乎想出了窍门,但我在努力解决其他问题.当我点击一个元素并打开它时,我想点击另一个元素以打开,而之前打开的元素以关闭.然而,它不起作用.每当我单击另一个元素时,打开的元素只会关闭,而不会打开另一个元素.基本上,我只想在单击该特定元素时关闭该元素.或者,当我点击另一个按钮时(这很管用),但在这种情况下,如果有意义的话,我也希望另一个打开.我在这一点上卡住了.

谢谢你的帮助.

这是我的密码笔:https://codepen.io/danosvk/pen/JjLEGMK

这是我的代码:

    const questions = document.querySelectorAll("section");
    const answers = document.querySelectorAll(".answer");
    
    toggle = false
    function open() {
      for (let i = 0; i < answers.length; i++) {
        answers[i].style.display = "none";
      }
    toggle = !toggle
      this.lastElementChild.style.display = toggle ? "block" : "none"
    }
    
    questions.forEach((question) => question.addEventListener("click", open));
    .card {
      width: 20.4375rem;
      background-color: #ffffff;
      border-radius: 1.4375rem;
      margin: auto;
      padding: 132px 24px 48px;
      text-align: center;
      transform: translateY(-125px);
    }
    
    section {
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #e8e8ea;
      flex-wrap: wrap;
    }
    
    p {
      color: var(--main-text-color);
      font-size: 0.75rem;
    }
    
    .answer {
      flex-basis: 100%;
      text-align: left;
      display: none;
    }
    <div class="card">
      <h1 class="no">faq</h1>
      <section>
        <p class="question">How many team members can i invite?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          You can invite up to 2 additional users on the Free plan. There is no
          limit on team members for the Premium plan.
        </p>
      </section>
      <section>
        <p class="question">What is the maximum file upload size?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          No more than 2GB. All files in your account must fit your allotted
          storage space.
        </p>
      </section>
      <section>
        <p class="question">How do I reset my password?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          Click “Forgot password” from the login page or “Change password” from
          your profile page. A reset link will be emailed to you.
        </p>
      </section>
      <section>
        <p class="question">Can I cancel my subscription?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          Yes! Send us a message and we’ll process your request no questions
          asked.
        </p>
      </section>
      <section>
        <p class="question">Do you provide additional support?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          Chat and email support is available 24/7. Phone lines are open during
          normal business hours.
        </p>
      </section>

推荐答案

使用toggle不起作用,因为当答案显示时总是false.

相反,保存单击选项before的样式,擦除所有答案,然后使用它来确定是否显示答案.

如果this.lastElementChild.style.display''(即第一次单击菜单时),则将其设置为none.

    const questions = document.querySelectorAll("section");
    const answers = document.querySelectorAll(".answer");
    
    function open() {

      currentDisplay=this.lastElementChild.style.display||'none';

      for (let i = 0; i < answers.length; i++) {
        answers[i].style.display = "none";
      }

      if (currentDisplay=='none') this.lastElementChild.style.display = "block"

    }
    
    questions.forEach((question) => question.addEventListener("click", open));
    .card {
      width: 20.4375rem;
      background-color: #ffffff;
      border-radius: 1.4375rem;
      margin: auto;
      padding: 132px 24px 48px;
      text-align: center;
      transform: translateY(-125px);
    }
    
    section {
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #e8e8ea;
      flex-wrap: wrap;
    }
    
    p {
      color: var(--main-text-color);
      font-size: 0.75rem;
    }
    
    .answer {
      flex-basis: 100%;
      text-align: left;
      display: none;
    }
    <div class="card">
      <h1 class="no">faq</h1>
      <section>
        <p class="question">How many team members can i invite?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          You can invite up to 2 additional users on the Free plan. There is no
          limit on team members for the Premium plan.
        </p>
      </section>
      <section>
        <p class="question">What is the maximum file upload size?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          No more than 2GB. All files in your account must fit your allotted
          storage space.
        </p>
      </section>
      <section>
        <p class="question">How do I reset my password?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          Click “Forgot password” from the login page or “Change password” from
          your profile page. A reset link will be emailed to you.
        </p>
      </section>
      <section>
        <p class="question">Can I cancel my subscription?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          Yes! Send us a message and we’ll process your request no questions
          asked.
        </p>
      </section>
      <section>
        <p class="question">Do you provide additional support?</p>
        <img class="arrow" src="./images/icon-arrow-down.svg" alt="" />
        <p class="answer">
          Chat and email support is available 24/7. Phone lines are open during
          normal business hours.
        </p>
      </section>

Javascript相关问答推荐

我不明白这个react 组件有什么问题

用户单击仅在JavaScript中传递一次以及其他行为

从实时数据库(Firebase)上的子类别读取数据

网页自检测外部元素无法加载

InDesign—创建一个独立的窗口,在文档中进行更正时保持打开状态

阿波罗返回的数据错误,但在网络判断器中是正确的

如何根据当前打开的BottomTab Screeb动态加载React组件?

使用GraphQL查询Uniswap的ETH价格

为什么我的导航条打开状态没有在窗口addeventlistener(Reaction Js)中更新?

Reaction Native中的范围滑块

Next.js服务器端组件请求,如何发送我的cookie token?

第二次更新文本输入字段后,Reaction崩溃

从另一个数组中的对应行/键值对更新数组中的键值对对象

当代码另有说明时,随机放置的圆圈有时会从画布上消失

OnClick更改Json数组JSX中的图像源

如何在脚本编译后直接将RxJ模块导入浏览器(无需Angel、webpack、LiteServer)

如何在下一个js中更改每个标记APEXCHARTS图表的 colored颜色

ReferenceError:无法在初始化之前访问setData

Plotly.js栏为x轴栏添加辅助文本

扩散运算符未按预期工作,引发语法错误