使用jQuery slideToggle一组Table Rows

CCC*_*CCC 7 javascript jquery html-table slidetoggle

我是javaScript和jQuery的新手,所以希望这将是一个快速解决方案.我需要显示一个包含数据的表格,这些数据可以分为几个类别,我想实现一个slideToggle,它隐藏/显示每个给定类别中的所有观察结果.

下面的代码应该(理想情况下)显示一个包含4列和9行的表,每组3行,前面是绿色的"Section i"行.我希望每个Section标头都可以作为一个slideToggle来扩展或折叠它下面的所有行.现在,没有任何东西在崩溃.有什么想法吗?

<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>
Run Code Online (Sandbox Code Playgroud)

mu *_*ort 5

您不能将<div>s 混合成<table>类似的,<tbody>而是使用其他元素.在你的回调中,this<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>
        <!-- ... -->
Run Code Online (Sandbox Code Playgroud)

你的点击处理程序如下:

$('.flip').click(function() {
    $(this)
        .closest('tbody')
        .next('.section')
        .toggle('fast');
});
Run Code Online (Sandbox Code Playgroud)

.closest呼叫追溯到你的祖先,直到找到<tbody>然后调用.next上.

更新了jsfiddle:http://jsfiddle.net/ambiguous/Udxyb/4/