我使用这个脚本成功地得到了一个表来折叠。我正在试图弄清楚如何在行的其余部分保持折叠时,只使第一行保持展开。
现在我的剧本已经崩溃了。我不知道如何指定展开第一行,而其他行则折叠。
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$('.container tr:not(.header)').slideToggle(0, function(){});
$('.header').click(function(){
$(this).find('span').text(function(_, value){return value=='-'?'+':'-'});
$(this).nextUntil('tr.header').find('tr').slideToggle(0);
$(this).nextUntil('tr.header').slideToggle(10, function(){});
});
</script>发布于 2014-05-01 19:32:17
要获得您想要的--即第一个表未折叠--您可以在第一个表中添加一个额外的类,并在文档准备就绪时这样调用它。
$(document).ready(function(){
$('.first').nextUntil('tr.header').slideToggle(0);
});看这把小提琴:http://jsfiddle.net/g67AH/20
您可以使用CSS :not和:first-child选择器。这将确保折叠的tr元素不是第一个子元素。
$('.header').click(function(){
$(this).find('span').text(function(_, value){return value=='-'?'+':'-'});
$(this).nextUntil('tr.header').find('tr:not(:first-child)').slideToggle(0);
$(this).nextUntil('tr.header:not(:first-child)').slideToggle(10, function(){});
});另一个选项是为您的表设置一个标题行,如下所示:
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row1 column 1</td>
<td>Row1 column 2</td>
<td>Row1 column 3</td>
<td>Row1 column 4</td>
</tr>
</tbody>
</table>然后,您所需要做的就是在.slideToggle()上调用tbody,您的标题应该保持不变。
有关复制这两种建议的示例,请参见此小提琴
https://stackoverflow.com/questions/23411613
复制相似问题