在我的网页上,我想“隐藏”或“取消隐藏”DIV 元素内已有的几个元素(本例中为 C 和 F), 如:
<div> Select A or B <span name='hide' > or C</span></div>
<div> Also select D or E <span name='hide' > or F</span></div>
(etc)
当页面打开时,我使用 javascript 隐藏所有“隐藏”元素,除非在本地主机上打开页面,否则所有元素都会显示。 我不一定知道有多少“隐藏”元素(动态生成)。
var hids=document.getElementsByName('hide');
if(hids!=null) {
for(var j=0; j< hids.length; j++) {
if(localhost==true) { // only if on localhost
hids[j].style.visibility='visible';
}
else hids[j].style.visibility='hidden';
}
}
但是,“name”属性对于 SPAN 无效。当我使用 DIV 而不是 SPAN 时,它会弄乱格式。我该如何正确解决这个问题?
最佳答案
使用class
代替name
:
<span class="my-class"> or C</span>
和getElementsByClassName
而不是getElementsByName
:
document.getElementsByClassName("my-class");
关于javascript - 我需要选择多个 SPAN 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48587581/