英文:
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="toggleSidebar()"> </button>
function toggleSidebar() {
document.getElementsByClassName("sidebar")[0].classList.toggle("active");
}
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("side1Toggle").addEventListener("click", toggleSidebar1);
document.getElementById("side2Toggle").addEventListener("click", toggleSidebar2);
function toggleSidebar1() {
// If there is a specific single element you want to work with use .querySelector():
document.querySelector(".sidebar1").classList.toggle("active");
}
function toggleSidebar2() {
// If you want to work with all the matches use .querySelectorAll():
document.querySelectorAll(".sidebar2").forEach(function(sidebar){
sidebar.classList.toggle("active");
});
}
<!-- language: lang-css -->
.active { color:red; }
<!-- language: lang-html -->
<button id="side1Toggle">Click to toggle sidebar 1 Class</button>
<div class="sidebar1">This is sidebar1.</div>
<button id="side2Toggle">Click to toggle sidebar 2 Class</button>
<div class="sidebar2">This is sidebar2.</div>
<div class="sidebar2">This is sidebar2.</div>
<div class="sidebar2">This is sidebar2.</div>
<div class="sidebar2">This is sidebar2.</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论