[英]JavaScript classlist selector error
I'm getting this error "app_copy.js:13 Uncaught TypeError: Cannot read property 'remove' of undefined" and am not sure why. 我收到此错误“ app_copy.js:13 Uncaught TypeError:无法读取未定义的属性'remove'”,并且不确定原因。 When you click on the first button I want those buttons removed and two hidden buttons to show. 当您单击第一个按钮时,我希望这些按钮被删除,并显示两个隐藏的按钮。 Why am I getting this error when I click on the first button? 为什么在单击第一个按钮时出现此错误? It works if I use document.querySelector and remove a single one but not if I use document.querySelectorAll like I'm trying to do here. 如果我使用document.querySelector并删除一个,它会起作用,但是如果我使用document.querySelectorAll,例如我在这里尝试,则不会。 Thanks! 谢谢!
Codepen: http://codepen.io/abharms/pen/BKvYvL Codepen: http ://codepen.io/abharms/pen/BKvYvL
HTML 的HTML
<div class="wrapper">
<a class="numberButtons first" href="#">1</a>
<a class="numberButtons second" href="#">2</a>
<a class="numberButtons third" href="#">3</a>
<a class="yesNo hide" href="#">Yes</a>
<a class="yesNo hide" href="#">No</a>
</div>
CSS 的CSS
body {
background-color: #74c7d5;
}
.wrapper {
text-align: center;
margin-top: 200px;
}
.wrapper a {
text-decoration: none;
padding: 10px 15px 10px 15px;
margin: 10px;
border: 1px solid white;
color: #9965a8;
border-radius: 5px;
}
.wrapper a:hover {
background-color: white;
color: #74c7d5;
}
.hide {
display: none;
}
JavaScript 的JavaScript
var numberButtons = document.querySelectorAll(".numberButtons");
var yesNo = document.querySelectorAll(".yesNo");
var first = document.querySelector(".first");
var second = document.querySelector(".second");
var third = document.querySelector(".third");
function numberButtonsLoop() {
for(var i = 0; i < numberButtons.length; i++)
numberButtons[i].addEventListener("click", function(){
var clickedOption = this;
if(clickedOption === first) {
yesNo.classList.remove("yesNo");
numberbuttons.classList.add("hide");
}
});
};
numberButtonsLoop();
Your variables yesNo
, and numberbuttons
, each hold a collection of HTML element nodes returned from document.querySelectorAll()
, even if those collection are comprised of only one element; 您的变量yesNo
和numberbuttons
都包含从document.querySelectorAll()
返回的HTML元素节点的集合,即使这些集合仅包含一个元素也是如此; to remove the class-name from all elements in that collection you'd have to iterate over each element node in turn: 要从该集合中的所有元素中删除类名,您必须依次遍历每个元素节点:
if(clickedOption === first) {
// Array.from() converts the array-like collection
// into an Array upon which we can use the
// Array.prototype.forEach() method to iterate over
// the newly-formed Array:
Array.from( yesNo ).forEach(function(element){
element.classList.remove("yesNo");
});
Array.from( numberbuttons ).forEach(function(element){
element.classList.add("hide");
});
}
Due to the query selectors returning an array of elements (since you are selecting by className), you need to loop through all elements in the array when you remove/add the class. 由于查询选择器返回元素数组(因为您是按className选择的),因此在删除/添加类时,需要遍历数组中的所有元素。
This error was present for both adding a class name to the numberButtons and yesNo elements. 将类名添加到numberButtons和yesNo元素时均出现此错误。 I have fixed both in my snippet. 我已经修复了这两个片段。
var numberButtons = document.querySelectorAll(".numberButtons"); var yesNo = document.querySelectorAll(".yesNo"); var first = document.querySelector(".first"); var second = document.querySelector(".second"); var third = document.querySelector(".third"); function numberButtonsLoop() { for(var i = 0; i < numberButtons.length; i++) numberButtons[i].addEventListener("click", function(){ var clickedOption = this; if(clickedOption === first) { for(var i=0; i<yesNo.length; i++) { yesNo[i].classList.remove("yesNo"); } for(var i=0; i<numberButtons.length; i++) { numberButtons[i].classList.add("hide"); } } }); }; numberButtonsLoop();
body { background-color: #74c7d5; } .wrapper { text-align: center; margin-top: 200px; } .wrapper a { text-decoration: none; padding: 10px 15px 10px 15px; margin: 10px; border: 1px solid white; color: #9965a8; border-radius: 5px; } .wrapper a:hover { background-color: white; color: #74c7d5; } .hide { display: none; }
<div class="wrapper"> <a class="numberButtons first" href="#">1</a> <a class="numberButtons second" href="#">2</a> <a class="numberButtons third" href="#">3</a> <a class="yesNo hide" href="#">Yes</a> <a class="yesNo hide" href="#">No</a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.