日期:2014-05-16  浏览次数:20369 次

关于这个问题的.style.display ,求一个函数算法.....谢谢...........................
<table   id=v1   style=display:none>
....
</table>
<table   id=v2   style=display:none>
....
</table>
<table   id=v3   style=display:none>
....
</table>
<table   id=v4   style=display:none>
....
</table>

怎样才能只是打开一个table.其他的table处于关闭状态.就是说不能同时有2个table打开

------解决方案--------------------
function showtable(s)
{
for(var i=1;i <=4;i++)
{
var obj = document.getElementById( "v " + i);
if(s == i)
obj.style.display = "none ";
else
obj.style.display = " ";
}
}
------解决方案--------------------
<table id=v1 style=display:none>
....
</table>
<table id=v1 style=display:none>
....
</table>
<table id=v1 style=display:none>
....
</table>
<table id=v1 style=display:none>
....
</table>

function showtable(s)
{
var aa = document.getElementsByName( "v1 ");
for(var i=1;i <aa.length;i++)aa[i].style.display = "none ";
a[s].style.display= " ";
}
------解决方案--------------------
两种思想:
1,可以象上面朋友写的那样,每次将要显示的块的style.display= "block ",其余的用循环设置style.display= "none ".但这样有个明显的缺点是执行效率比较低。但也有个优点是比较可靠,可以确保一组中只有一个被显示。
2,先将所要求的一组元素其中一个显示,其余的隐藏,并用一个变量(叫做当前变量)保存当前显示的元素的id.
然后要显示其它同组元素时,只需要隐藏当前元素,显示目标元素,并改变当前变量的值。例子如下: