javascript – 无法使用getElementsByName()在IE中切换表行的显示
作者:互联网
我想根据选择框的值显示和隐藏表的行,这适用于Firefox但不适用于IE:
<select onChange="javascript: toggle(this.value);">
<option value="cat0">category 0</option>
<option value="cat1">category 1</option>
</select>
<table>
<tr name="cat0">
<td>some stuff v</td>
<td>some stuff v</td>
</tr>
<tr name="cat0">
<td>some stuff d</td>
<td>some stuff d</td>
</tr>
<tr name="cat1">
<td>some stuff a</td>
<td>some stuff a</td>
</tr>
<tr name="cat1">
<td>some stuff b</td>
<td>some stuff b</td>
</tr>
</table>
<script type="text/javascript">
function toggle(category)
{
// turn everything off
for (var i = 0; i < 2; i++)
{
var cat = document.getElementsByName('cat' + i);
for (var j = 0; j < cat.length; j++)
cat[j].style.display = 'none';
}
// turn on category chosen
var cat = document.getElementsByName(category);
for (var i = 0; i < cat.length; i++)
cat[i].style.display = '';
}
// start by showing cat0
toggle('cat0');
</script>
解决方法:
IE不允许您使用document.getElementsByName方法访问表行.如果您使用ID而不是名称,它将起作用.有关您正在寻找的代码,请参阅此页面:http://www.toolazy.me.uk/template.php?content=getelementsbyname.xml
标签:html,dhtml,javascript 来源: https://codeday.me/bug/20190730/1582445.html