问题描述
我正在尝试建立一个脚本,该脚本使用特定的类名将所有内容设置为不可见.这是我要呼叫的示例:
I'm trying to set up a script that sets invisible everything with a certain class name.This is an example of what I'm trying to call:
<script type="text/javascript">
function hideItems(){
document.getElementsByClassName('class1').style.visibility = "hidden";
}
</script>
类名在表的维度上,类似于此示例:
The class names are on the dimensions of a table, similar to this example:
<table onclick="hideItems()" width="200" border="1">
<tr>
<td class="class1">1</td>
<td class="class2">2</td>
<td class="class3">3</td>
<td class="class1">1</td>
<td class="class2">2</td>
<td class="class3">3</td>
</tr>
<tr>
<td class="class3">3</td>
<td class="class1">1</td>
<td class="class2">2</td>
<td class="class3">3</td>
<td class="class1">1</td>
<td class="class2">2</td>
</tr>
</table>
最后,将有三个复选框,根据选择的三个维度显示尺寸.那部分我可以做得很好,但是调用特定的尺寸以使其不可见是我目前遇到的问题.
In the end, there's going to be a three checkboxes, displaying the dimensions based on which of the three are selected. That part, I can do just fine, but calling the particular dimensions to become invisible is what I'm having an issue with currently.
在此先感谢您的帮助.
推荐答案
getElementsByClassName
返回一个集合.除非您使用jquery
getElementsByClassName
returns a collection. You can't collectively set properties unless you're using a framwork like jquery
var elems = document.getElementsByClassName('class1');
for(var i = 0; i != elems.length; ++i)
{
elems[i].style.visibility = "hidden"; // hidden has to be a string
}
这篇关于JS:通过getElementsByClassName使某些元素隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!