我对 javaScript 和 jQuery 相当陌生,所以希望这将是一个快速修复。我需要显示一个包含可分为多个类别的数据的表格,并且我想实现一个隐藏/显示每个给定类别中的所有观察结果的幻灯片切换。
下面的代码(理想情况下)应显示一个 4 列 9 行的表格,每组 3 行前面有一个绿色的“Section i”行。我希望每个节标题都作为幻灯片切换来展开或折叠其下方的所有行。现在,什么都没有崩溃。有什么想法吗?
<head>
<style type="text/css">
td{padding:5px;}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip").click(function(){
$(this).next(".section").slideToggle();
});
});
</script>
</head>
<body>
<p>
<table id="main_table">
<thead>
<tr class="firstline">
<th>Column1</th>
<th>Column2</th>
<th>Column3</th>
<th>Column4</th>
</tr>
</thead>
<tbody>
<tr style="background-color:green; color:white">
<td colspan="4" class="flip"> Section 1 </td>
</tr>
<div class="section">
<tr>
<td>item 111</td>
<td>item 112</td>
<td>item 113</td>
<td>item 114</td>
</tr>
<tr>
<td>item 121</td>
<td>item 122</td>
<td>item 123</td>
<td>item 124</td>
</tr>
<tr>
<td>item 131</td>
<td>item 132</td>
<td>item 133</td>
<td>item 134</td>
</tr>
</div>
<tr style="background-color:green; color:white">
<td colspan="4" class="flip"> Section 2 </td>
</tr>
<div class="section">
<tr>
<td>item 211</td>
<td>item 212</td>
<td>item 213</td>
<td>item 214</td>
</tr>
<tr>
<td>item 221</td>
<td>item 222</td>
<td>item 223</td>
<td>item 224</td>
</tr>
<tr>
<td>item 231</td>
<td>item 232</td>
<td>item 233</td>
<td>item 234</td>
</tr>
</div>
<tr style="background-color:green; color:white">
<td colspan="4" class="flip"> Section 3 </td>
</tr>
<div class="section">
<tr>
<td>item 311</td>
<td>item 312</td>
<td>item 313</td>
<td>item 314</td>
</tr>
<tr>
<td>item 321</td>
<td>item 322</td>
<td>item 323</td>
<td>item 324</td>
</tr>
<tr>
<td>item 331</td>
<td>item 332</td>
<td>item 333</td>
<td>item 334</td>
</tr>
</div>
</tbody>
</table>
</p>
</body>
你不能混合<div>
变成一个<table>
像这样,使用额外的<tbody>
元素代替。在你的回调中,this
is the <td>
没有兄弟姐妹的元素,所以.next
没有做任何有用的事情;你想要返回,直到到达与之前深度相同的地方.section
您感兴趣,然后致电.next
从那里。
您的 HTML 应该看起来更像这样:
<table id="main_table">
<thead>
<tr class="firstline">
<th>Column1</th>
<th>Column2</th>
<th>Column3</th>
<th>Column4</th>
</tr>
</thead>
<tbody>
<tr style="background-color:green; color:white">
<td colspan="4" class="flip"> Section 1 </td>
</tr>
</tbody>
<tbody class="section">
<tr>
<td>item 111</td>
<td>item 112</td>
<td>item 113</td>
<td>item 114</td>
</tr>
<!-- ... -->
你的点击处理程序如下所示:
$('.flip').click(function() {
$(this)
.closest('tbody')
.next('.section')
.toggle('fast');
});
The .closest调用会追溯到你的祖先,直到找到一个<tbody>
然后你打电话.next
关于这一点。
更新了jsfiddle:http://jsfiddle.net/ambiguously/Udxyb/4/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)