admin管理员组文章数量:1287970
I have a little Javascript problem. Instead of using this:
document.getElementById("hoverinv1").style.display = "";
document.getElementById("hoverinv2").style.display = "";
document.getElementById("hoverinv3").style.display = "";
document.getElementById("hoverinv4").style.display = "";
document.getElementById("hoverinv5").style.display = "";
document.getElementById("hoverinv6").style.display = "";
document.getElementById("hoverinv7").style.display = "";
document.getElementById("hoverinv8").style.display = "";
document.getElementById("hoverinv9").style.display = "";
document.getElementById("hoverinv10").style.display = "";
I wanted to use this:
for (var x = 0; x < 11; x++) {
document.getElementById("hoverinv" + x).style.display="";
}
To display again everything. Well, it does nothing and I have no idea whats the problem.
I have a little Javascript problem. Instead of using this:
document.getElementById("hoverinv1").style.display = "";
document.getElementById("hoverinv2").style.display = "";
document.getElementById("hoverinv3").style.display = "";
document.getElementById("hoverinv4").style.display = "";
document.getElementById("hoverinv5").style.display = "";
document.getElementById("hoverinv6").style.display = "";
document.getElementById("hoverinv7").style.display = "";
document.getElementById("hoverinv8").style.display = "";
document.getElementById("hoverinv9").style.display = "";
document.getElementById("hoverinv10").style.display = "";
I wanted to use this:
for (var x = 0; x < 11; x++) {
document.getElementById("hoverinv" + x).style.display="";
}
To display again everything. Well, it does nothing and I have no idea whats the problem.
Share Improve this question edited Nov 10, 2014 at 15:45 But I'm Not A Wrapper Class 14.3k6 gold badges47 silver badges67 bronze badges asked Nov 10, 2014 at 15:40 ShigetoraShigetora 331 gold badge1 silver badge4 bronze badges 3- 2 give your elements a class instead? – Patsy Issa Commented Nov 10, 2014 at 15:41
-
4
x
should start from1
not0
– Alex Char Commented Nov 10, 2014 at 15:42 - 3 Look at the console. – SLaks Commented Nov 10, 2014 at 15:43
3 Answers
Reset to default 6It's probably throwing an error on the first iteration because hoverinv0 does not exist. You want
for (var x = 1; x < 11; x++) {
document.getElementById("hoverinv" + x).style.display="";
}
Your first code starts with "hoverinv1"
and in your loop you start with "hoverinv0"
.
Probably, it is not finding the element, returning undefined
and then your script crashes when you try to access style
property.
Check the browser's console, it should emit an error or something.
Do it simply in CSS:
[id^=hoverinv] {
display: block; /* or any other needed display value */
}
Your issue: by inspecting your JS in Console you can clearly see that it breaks while fetching your hoverinv0
ID element (does not exists).
What you can:
assign a class class="hoverinv"
to all your element and go for:
var hoverInv = document.getElementsByClassName("hoverinv");
for (var i=0; i<hoverInv.length; i++){
hoverInv[i].style.display = "";
}
or using
var hoverInv = document.querySelectorAll("[id^=hoverinv]");
for (var i=0; i<hoverInv.length; i++){
hoverInv[i].style.display = "";
}
or something you might go fix all over again as soon you change the number of elements:
for(var i=1; i<11; i++){
document.getElementById("hoverinv"+ i).style.display = "";
}
本文标签: For Loop in Javascript (documentgetElementById)Stack Overflow
版权声明:本文标题:For Loop in Javascript (document.getElementById) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329985a2372714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论