用tbody代替div 解决 table tr的隐藏问题

来源:互联网 发布:javascript not 编辑:程序博客网 时间:2024/05/21 19:29
 

有如下需求,需要控制一个table内几个tr的显示问题。一开始想的方法是在这几个要显示的tr外面套一个div,利用div的display:none属性来解决。 但是后来发现div和tr嵌套的时候会有问题。例如,如果我的html页面是这样的源码:view plaincopy to clipboardprint?

<html>
<head>  
</head>  
<body>                      
<table>  
<div id="borrowAccountDiv_CJ" style="display:none;" mce_style="display:none;">  
<tr>  
<td>借方帐号:</td>  
</tr>  
<tr>  
<td><input type="text" id="borrowAcount" name="borrowAcount" style="width:200px;" dataType="MoneyRequire" msg="借方帐号不能够为空,且必须为数字!"onclick="this.select()"/><fontcolor="#FF0000"> *</font></td>  
</tr>  
</div>  
</table>  
</body>
</html>

那么打开这个html页面后,发现DIV中的tr还是会显示。

 

后来才发现,其实div和tr的相互嵌套是有问题。所以可以用tbody来代替实现。实现后的代码如下:

view plaincopy to clipboardprint?
<html>

<head>
</head>
<body>     
<table>
<tbody id="borrowAccountDiv_CJ" style="display:none;" mce_style="display:none;">
<tr>
<td>借方帐号:</td>
</tr>
<tr>
<td><input type="text" id="borrowAcount" name="borrowAcount" style="width:200px;" dataType="MoneyRequire" msg="借方帐号不能够为空,且必须为数字!"onclick="this.select()"/><fontcolor="#FF0000"> *</font></td>
</tr>
</tbody>
</table>
</body>

</html>

 

如果要分别隐藏多段tr,可以在table加入多个tbody,进行分别隐藏。table是支持多个tbody的哟。

原创粉丝点击