Jquery 折叠表的列
我有一个包含5列的表格。我想在单击图像时折叠表格的最后三列,然后在单击其他图像时再次显示。我已经编写了一些代码,但不起作用,因此请指导我:Jquery 折叠表的列,jquery,Jquery,我有一个包含5列的表格。我想在单击图像时折叠表格的最后三列,然后在单击其他图像时再次显示。我已经编写了一些代码,但不起作用,因此请指导我: <img id="hide" src="assets/img/decrease_indent.png" /> <img id="show" src="assets/img/increase_indent.png" /> <thead> <tr>
<img id="hide" src="assets/img/decrease_indent.png" />
<img id="show" src="assets/img/increase_indent.png" />
<thead>
<tr>
<th class="">Name</th>
<th class=""></th>
<th class="">Dur</th>
<th class="">Start</th>
<th class=""></th>
</tr>
</thead>
<tbody data-bind="foreach:items">
<tr data-bind="value:id">
<td data-bind="text:"></td>
<td></td>
<td data-bind="text:"></td>
<td data-bind="text:"></td>
<td data-bind="text:"></td>
</tr>
</tbody>
<script type="text/javascript">
$(function(){
$("#hide").live('click',function(){
$("th:eq(2),th:eq(3),th:eq(4)td:eq(2),td:eq(3),td:eq(4)").hide();
});
$("#show").live('click',function(){
$("th:eq(2),th:eq(3),th:eq(4)td:eq(2),td:eq(3),td:eq(4)").show();
});
});
</script>
名称
杜尔
开始
$(函数(){
$(“#隐藏”).live('单击',函数()){
$(“th:eq(2)、th:eq(3)、th:eq(4)td:eq(2)、td:eq(3)、td:eq(4)”).hide();
});
$(“#显示”).live('单击',函数()){
$(“th:eq(2)、th:eq(3)、th:eq(4)td:eq(2)、td:eq(3)、td:eq(4)”)。show();
});
});
试试这个
//To hide
$('th:gt(2)').hide();
$('td:gt(2)').hide();
//To show
$('th:gt(2)').show();
$('td:gt(2)').show();
根据评论进行编辑
在隐藏和显示中,在
th:eq(4)
和td:eq(2)
之间缺少一个逗号
//To hide
$('th:gt(2)').hide();
$('td:gt(2)').hide();
//To show
$('th:gt(2)').show();
$('td:gt(2)').show();
基于您的代码的工作示例可以在这里看到
请注意,在使用
.live
时,您不能使用最新版本的jQuery。您的jQuery代码应该包装在$(document).ready(function(){//将所有jQuery代码放在这里。})
可能重复的表内容应该包装在中,您的表内容放在这里
是的,如果您有答案,我知道很多,然后回复@muleskinner请注意,最新版本的jQuery不再支持.live
。很难说这是否是问题所在,因为您没有提到您使用的版本。您可以将代码(包括有效的html)发布到JSFIDLE吗?但是由于此代码,我的第一行正在折叠。我想对最后三行执行此操作您想折叠列还是行?检查有问题的语句“我想折叠表的最后三列”这在您之前的评论中是什么意思“但是由于这段代码,我的第一行正在折叠。我想对最后三行执行此操作”这意味着我的所有5个表标题正在折叠,这意味着所有列中的整个第一行都没有折叠?