为什么每当我按下按钮时都会收到这个错误?

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

Why do I keep getting this error when I press a button?

问题

无法读取未定义属性 'toggle' 的未捕获的 TypeError 错误

这是代码:

HTML

JavaScript:

function toggleSidebar() {
document.getElementsByClassName("sidebar").classList.toggle("active");
}

当我使用 console.log 检查按钮是否被按下时,按钮似乎正常工作。但是,当我运行上述代码时,我一直收到该错误。帮助将不胜感激!

英文:

Uncaught TypeError: Cannot read property 'toggle' of undefined

Here's the code:

HTML

<button onclick="toggleSidebar()"> </button>

JavaScript:

function toggleSidebar() {
 document.getElementsByClassName("sidebar").classList.toggle("active");
}

The button seems to be working fine when I console.log to check if it's being pressed. But, when I run the above code I keep getting that error. Help would be greatly appreciated!

答案1

得分: -1

.getElementsByClassName() 返回匹配元素的集合。因此,首先您必须有一些具有指定类的元素。

接下来,集合没有classList属性。您必须隔离单个元素以使用classList,可以通过将索引传递给返回的集合来实现:

<button onclick="toggleSidebar()"></button>

function toggleSidebar() {
 document.getElementsByClassName("sidebar")[0].classList.toggle("active");
}

话虽如此,您首先不应该使用.getElementsByClassName()。而是使用其现代对应物.querySelectorAll()。另外,不应该使用内联HTML事件属性来设置事件,例如onclick。事件应该在JavaScript中设置。

// 获取按钮的引用并设置点击事件处理程序
document.getElementById("side1Toggle").addEventListener("click", toggleSidebar1);
document.getElementById("side2Toggle").addEventListener("click", toggleSidebar2);

function toggleSidebar1() {
  // 如果您想要使用特定的单个元素,请使用.querySelector():
  document.querySelector(".sidebar1").classList.toggle("active");
}

function toggleSidebar2() {
  // 如果您想要处理所有匹配项,请使用.querySelectorAll():
  document.querySelectorAll(".sidebar2").forEach(function(sidebar){
    sidebar.classList.toggle("active");
  });
}
.active { color:red; }
<button id="side1Toggle">点击切换侧边栏 1 类</button>
<div class="sidebar1">这是侧边栏 1。</div>

<button id="side2Toggle">点击切换侧边栏 2 类</button>
<div class="sidebar2">这是侧边栏 2。</div>
<div class="sidebar2">这是侧边栏 2。</div>
<div class="sidebar2">这是侧边栏 2。</div>
<div class="sidebar2">这是侧边栏 2。</div>
英文:

.getElementsByClassName() returns a collection of matching elements. So, first you must have some elements that have the class specified.

Next, collections don't have a classList property. You must isolate an individual element to use classList, which you can do by passing an index to the returned collection:

button onclick=&quot;toggleSidebar()&quot;&gt; &lt;/button&gt;

function toggleSidebar() {
 document.getElementsByClassName(&quot;sidebar&quot;)[0].classList.toggle(&quot;active&quot;);
}

With that said, you should not be using .getElementsByClassName() in the first place. Instead, use its modern counterpart, .querySelectorAll(). Additionally, you should not be setting up your events with inline HTML event attributes, like onclick. Events should be set up in JavaScript.

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

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

// Get reference to the buttons and set up click event handlers
document.getElementById(&quot;side1Toggle&quot;).addEventListener(&quot;click&quot;, toggleSidebar1);
document.getElementById(&quot;side2Toggle&quot;).addEventListener(&quot;click&quot;, toggleSidebar2);

function toggleSidebar1() {
  // If there is a specific single element you want to work with use .querySelector():
  document.querySelector(&quot;.sidebar1&quot;).classList.toggle(&quot;active&quot;);
}

function toggleSidebar2() {
  // If you want to work with all the matches use .querySelectorAll():
  document.querySelectorAll(&quot;.sidebar2&quot;).forEach(function(sidebar){
    sidebar.classList.toggle(&quot;active&quot;);
  });
}

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

.active { color:red; }

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

&lt;button id=&quot;side1Toggle&quot;&gt;Click to toggle sidebar 1 Class&lt;/button&gt;
&lt;div class=&quot;sidebar1&quot;&gt;This is sidebar1.&lt;/div&gt;

&lt;button id=&quot;side2Toggle&quot;&gt;Click to toggle sidebar 2 Class&lt;/button&gt;
&lt;div class=&quot;sidebar2&quot;&gt;This is sidebar2.&lt;/div&gt;
&lt;div class=&quot;sidebar2&quot;&gt;This is sidebar2.&lt;/div&gt;
&lt;div class=&quot;sidebar2&quot;&gt;This is sidebar2.&lt;/div&gt;
&lt;div class=&quot;sidebar2&quot;&gt;This is sidebar2.&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 19:17:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611126.html
匿名

发表评论

匿名网友

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

确定