[英]How do I change multiple elements with same class name using Javascript?
Follow up to my previous question - I want to use a button to show / hide multiple elements with the same class name using JS, yet it appears that I can only change the first element with a certain class name, and all further elements with the same class on the page are ignored. 跟进我之前的问题 - 我想使用一个按钮来显示/隐藏使用JS的相同类名的多个元素,但似乎我只能更改具有某个类名的第一个元素,以及所有其他元素页面上的同一个类被忽略。
How do I fix this? 我该如何解决?
function designInfo() { document.getElementsByClassName("design")[0].style.display = "block"; document.getElementsByClassName("it")[0].style.display = "none"; document.getElementsByClassName("other")[0].style.display = "none"; } function itInfo() { document.getElementsByClassName("design")[0].style.display = "none"; document.getElementsByClassName("it")[0].style.display = "block"; document.getElementsByClassName("other")[0].style.display = "none"; } function allInfo() { document.getElementsByClassName("design")[0].style.display = "block"; document.getElementsByClassName("it")[0].style.display = "block"; document.getElementsByClassName("other")[0].style.display = "block"; }
.it {} .design {} .other {} .indent { margin: .5em 1em .5em 1em; }
<button onclick="designInfo()">show design stuff</button> <button onclick="itInfo()">show IT stuff</button> <button onclick="allInfo()">show all</button> <div class="indent"> <div class="it">• boring IT stuff</div> <div class="design">• cool design stuff</div> <div class="it">• it stuff and things</div> <div class="design">• design stuff</div> <div class="it">• it stuff and more</div> <div class="other">• more it stuff</div> <div class="other">• it stuff</div> </div>
You need to use a for-loop over all items you get from document.getElementsByClassName()
method as in following snippet: 您需要对来自
document.getElementsByClassName()
方法的所有项目使用for循环,如下面的代码段所示:
function setDisplay(className, displayValue) { var items = document.getElementsByClassName(className); for (var i=0; i < items.length; i++) { items[i].style.display = displayValue; } } function designInfo() { setDisplay("design", "block"); setDisplay("it", "none"); setDisplay("other", "none"); } function itInfo() { setDisplay("design", "none"); setDisplay("it", "block"); setDisplay("other", "none"); } function allInfo() { setDisplay("design", "block"); setDisplay("it", "block"); setDisplay("other", "block"); }
.it {} .design {} .other {} .indent { margin: .5em 1em .5em 1em; }
<button onclick="designInfo()">show design stuff</button> <button onclick="itInfo()">show IT stuff</button> <button onclick="allInfo()">show all</button> <div class="indent"> <div class="it">• boring IT stuff</div> <div class="design">• cool design stuff</div> <div class="it">• it stuff and things</div> <div class="design">• design stuff</div> <div class="it">• it stuff and more</div> <div class="other">• more it stuff</div> <div class="other">• it stuff</div> </div>
Update Also, it could be written with less code as below: 更新此外,它可以用更少的代码编写如下:
function filter(designDisp, itDisp, otherDisp) { setDisplay("design", designDisp); setDisplay("it", itDisp); setDisplay("other", otherDisp); } function setDisplay(className, displayValue) { var items = document.getElementsByClassName(className); for (var i=0; i < items.length; i++) { items[i].style.display = (displayValue? "block" : "none"); } }
.it {} .design {} .other {} .indent { margin: .5em 1em .5em 1em; }
<button onclick="filter(1,0,0)">show design stuff</button> <button onclick="filter(0,1,0)">show IT stuff</button> <button onclick="filter(1,1,1)">show all</button> <div class="indent"> <div class="it">• boring IT stuff</div> <div class="design">• cool design stuff</div> <div class="it">• it stuff and things</div> <div class="design">• design stuff</div> <div class="it">• it stuff and more</div> <div class="other">• more it stuff</div> <div class="other">• it stuff</div> </div>
It would be best to do this with classes that show/hide the element. 最好使用显示/隐藏元素的类来执行此操作。 Your problem is you are using the first index [0] of the matched array.
您的问题是您正在使用匹配数组的第一个索引[0]。 You need to loop through each element in the array to manipulate the element.
您需要遍历数组中的每个元素来操作元素。
Something like this should do the trick: 像这样的东西应该做的伎俩:
let els = document.querySelectorAll('.myclass');
els.forEach(function(el) {
el.classList.add('hidden');
});
You can do the same with el.classList.remove(className);
您可以使用
el.classList.remove(className);
执行相同的el.classList.remove(className);
to remove a class 删除一个类
Note: forEach is an ECMAScript 5 function 注意:forEach是一个ECMAScript 5函数
getElementsByClassName
return an array of elements. getElementsByClassName
返回一个元素数组。 You have to iterate them and update their style one by one 你必须迭代它们并逐个更新它们的样式
var elements = { design: document.getElementsByClassName("design"), it: document.getElementsByClassName("it"), other: document.getElementsByClassName("other") }; function toggle(type) { Object.keys(elements).forEach(function(key) { var els = elements[key]; var state = "none"; if (key === type) state = "block"; for (var i = 0; i < els.length; i++) { els[i].style.display = state; } }); }
.indent { margin: .5em 1em .5em 1em; }
<button onclick="toggle('design')">show design stuff</button> <button onclick="toggle('it')">show IT stuff</button> <button onclick="toggle('other')">show all</button> <div class="indent"> <div class="it">• boring IT stuff</div> <div class="design">• cool design stuff</div> <div class="it">• it stuff and things</div> <div class="design">• design stuff</div> <div class="it">• it stuff and more</div> <div class="other">• more it stuff</div> <div class="other">• it stuff</div> </div>
You're half way there: 你在那里的一半:
Use this function and pass through the class name and either 'block' or 'none' 使用此函数并传递类名并“阻止”或“无”
function toggle(className, state){
elementArray = document.getElementsByClassName(className);
for(var i = 0; i < elementArray.length; i++){
elementArray[i].style.display = state;
}
}
Easy solution, made together with jQuery
. 简单的解决方案,与
jQuery
一起制作。 I've provided just one function, which accepts an argument, which is the name of the elements you want to leave in the list. 我只提供了一个函数,它接受一个参数,这是你想要在列表中保留的元素的名称。
var elems = $('.indent').children('div'); function filter(v) { elems.filter(function() { $(this).show(); if (!$(this).hasClass(v) && v) { $(this).hide(); } }); }
.indent { margin: .5em 1em .5em 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="filter('design')">show design stuff</button> <button onclick="filter('it')">show IT stuff</button> <button onclick="filter()">show all</button> <div class="indent"> <div class="it">• boring IT stuff</div> <div class="design">• cool design stuff</div> <div class="it">• it stuff and things</div> <div class="design">• design stuff</div> <div class="it">• it stuff and more</div> <div class="other">• more it stuff</div> <div class="other">• it stuff</div> </div>
var X = document.querySelectorAll(".className");
for (var i = 0; i < X.length; i++) {
X[i].style.cssProperty = someNumber + "unitType";
}
Example: X[i].style.width = 100 + "%";
示例:
X[i].style.width = 100 + "%";
If you want it to apply random integers: 如果你想要它应用随机整数:
var X = document.querySelectorAll(".className");
for (var i = 0; i < X.length; i++) {
var rand = Math.floor(Math.random() * someNumber);
X[i].style.cssProperty = rand + "unitType";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.