JavaScript无法打开/关闭手风琴。

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

Javascript not opening/closing accordion

问题

我尝试了几种不同的方法,但无法确定为什么我的JS不能打开和关闭我的手风琴。请检查我的代码。您的帮助将不胜感激。

var acc = document.getElementsByClassName("accordion");

var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {

    this.classList.toggle("active");

    var panel = this.nextElementSibling;
    if (panel.style.display === "block") {
      panel.style.display = "none";
    } else {
      panel.style.display = "block";
    }
  });
}
<div class="wrap1">
  <button class="accordion">About Us Page Map</button>

  <div id="pan1" class="panel">
    <p>
      <a href="About.html" id="ppl">Personel section</a><br />
      <a href="About.html" id="ach">Acheivments</a><br />
      <a href="About.html" id="his">History</a><br />
    </p>
  </div>
</div>
body {
    text-align: center;
    background-color: #ffffff;
}

h1 {
    font: 40px courier new;
    text-align: center;
}
a {
    font: 25px arial;
    text-align:center;
}

.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 60%;
    text-align: center;
    border: none;
    outline: none;
    transition: 0.4s;

}
.active, .accordion:hover {
    background-color: #ccc;
}

.panel {
    padding: 0 18px;
    background-color: white;
    overflow: hidden;
    font: 16px Source Sans Pro;
    width:60%;
    margin:0 auto;
    display:none;
    height:200px auto

}

p{
    text-align:center;
}

我已添加了CSS,我确实有一个名为“accordion”的类,但在原始问题中没有显示,对不起!

英文:

I've tried a few different ways but can not tell why my.JS isn't opening and closing my accordion. Please check my code. Your help would be appreciated.

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

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

var acc = document.getElementsByClassName(&quot;accordion&quot;);

var i;

for (i = 0; i &lt; acc.length; i++) {
  acc[i].addEventListener(&quot;click&quot;, function() {

    this.classList.toggle(&quot;active&quot;);


    var panel = this.nextElementSibling;
    if (panel.style.display === &quot;block&quot;) {
      panel.style.display = &quot;none&quot;;
    } else {
      panel.style.display = &quot;block&quot;;
    }
  });
}

/*this javascript seems to be fine and matches what some answers have said, and i've reset the page cache but i cannot seem to get it to work */
<!-- language: lang-html -->

&lt;div class=&quot;wrap1&quot;&gt;
  &lt;button class=&quot;accordion&quot;&gt;About Us Page Map&lt;/button&gt;

  &lt;div id=&quot;pan1&quot; class=&quot;panel&quot;&gt;
    &lt;p&gt;

      &lt;a href=&quot;About.html&quot; id=&quot;ppl&quot;&gt;Personel section&lt;/a&gt;&lt;br /&gt;
      &lt;a href=&quot;About.html&quot; id=&quot;ach&quot;&gt;Acheivments&lt;/a&gt;&lt;br /&gt;
      &lt;a href=&quot;About.html&quot; id=&quot;his&quot;&gt;History&lt;/a&gt;&lt;br /&gt;
    &lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

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

body {
    text-align: center;
    background-color: #ffffff;
}

h1 {
    font: 40px courier new;
    text-align: center;
}
a {
    font: 25px arial;
    text-align:center;
}

.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 60%;
    text-align: center;
    border: none;
    outline: none;
    transition: 0.4s;

}
.active, .accordion:hover {
    background-color: #ccc;
}

.panel {
    padding: 0 18px;
    background-color: white;
    overflow: hidden;
    font: 16px Source Sans Pro;
    width:60%;
    margin:0 auto;
    display:none;
    height:200px auto
    
}

p{
    text-align:center;
}

<!-- end snippet -->
I've added the css, i do have a class called accordion but it didn't show in the original question sorry!

答案1

得分: 1

问题在这里:

var acc = document.getElementsByClassName("accordion");

你的HTML文件中没有一个名为 "accordion" 的类,这就是为什么它无法识别它。你需要创建一个按钮,给它分配一个名为 "accordion" 的类,然后它就能正常工作了。

<button class="accordion">Personal</button>
<div class="panel">
  <p>Personal here</p>
</div>

<button class="accordion">Achievements</button>
<div class="panel">
  <p>Achievements here</p>
</div>

<button class="accordion">History</button>
<div class="panel">
  <p>History here</p>
</div>

希望这有所帮助。

英文:

The problem is here:

var acc = document.getElementsByClassName(&quot;accordion&quot;);

You don't have "accordion" class in your HTML file, that's why it is not able to recognize it. You have to take a button, assign it a class named accordion and then it will work properly.

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

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

var acc = document.getElementsByClassName(&quot;accordion&quot;);

var i;

for (i = 0; i &lt; acc.length; i++) {
  acc[i].addEventListener(&quot;click&quot;, function() {

    this.classList.toggle(&quot;active&quot;);


    var panel = this.nextElementSibling;
    if (panel.style.display === &quot;block&quot;) {
      panel.style.display = &quot;none&quot;;
    } else {
      panel.style.display = &quot;block&quot;;
    }
  });
}

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

.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}

.active,
.accordion:hover {
  background-color: #ccc;
}

.panel {
  padding: 0 18px;
  display: none;
  background-color: white;
  overflow: hidden;
}

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

&lt;button class=&quot;accordion&quot;&gt;Personal&lt;/button&gt;
&lt;div class=&quot;panel&quot;&gt;
  &lt;p&gt;Personal here&lt;/p&gt;
&lt;/div&gt;

&lt;button class=&quot;accordion&quot;&gt;Achievements&lt;/button&gt;
&lt;div class=&quot;panel&quot;&gt;
  &lt;p&gt;Achievements here&lt;/p&gt;
&lt;/div&gt;

&lt;button class=&quot;accordion&quot;&gt;History&lt;/button&gt;
&lt;div class=&quot;panel&quot;&gt;
  &lt;p&gt;History here&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

Source: HTML and CSS Accordion - w3schools

Hope this helps.

答案2

得分: 0

我建议重构一些你的代码,以使其更易读,比如

HTML

&lt;a href=&quot;#item1&quot; class=&quot;accordion&quot;&gt;项目1&lt;/a&gt;
&lt;div id=&quot;item1&quot; class=&quot;accordion-content&quot;&gt;这是我的折叠项1内容&lt;/div&gt;

&lt;a href=&quot;#item2&quot; class=&quot;accordion&quot;&gt;项目2&lt;/a&gt;
&lt;div id=&quot;item2&quot; class=&quot;accordion-content&quot;&gt;这是我的折叠项2内容&lt;/div&gt;

JS

document.querySelectorAll(&quot;.accordion&quot;).forEach(function(node) {
    node.addEventListener(&quot;click&quot;, function(e) {
        this.nextElementSibling.toggleClass(&quot;active&#39;);
    });
});

CSS

.accordion-content:not(.active) {
    display: none;
}

除了重构之外,我无法看到一个具有类名 accordion 的元素。看起来你缺少包含实际 折叠项 的HTML。

英文:

I would suggest refactoring some of your code to make it more readable such as

HTML

&lt;a href=&quot;#item1&quot; class=&quot;accordion&quot;&gt;Item 1&lt;/a&gt;
&lt;div id=&quot;item1&quot; class=&quot;accordion-content&quot;&gt;This is my accordion Item 1 content&lt;/div&gt;

&lt;a href=&quot;#item2&quot; class=&quot;accordion&quot;&gt;Item 2&lt;/a&gt;
&lt;div id=&quot;item2&quot; class=&quot;accordion-content&quot;&gt;This is my accordion Item 2 content&lt;/div&gt;

JS

document.querySelectorAll(&quot;.accordion&quot;).forEach(function(node) {
    node.addEventListener(&quot;click&quot;, function(e) {
        this.nextElementSibling.toggleClass(&quot;active&#39;);
    });
});

CSS

.accordion-content:not(.active) {
    display: none;
}

Besides the refactor, I'm unable to see an element with the className accordion. It seems you are missing the HTML containing the actual accordion.

huangapple
  • 本文由 发表于 2020年1月3日 15:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574897.html
匿名

发表评论

匿名网友

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

确定