如何编写jQuery来扩展/折叠行?

时间:2014-03-07 13:26:16

标签: javascript jquery html

这是我的HTML代码:http://jsfiddle.net/Udxyb/406/

在上面的jQuery代码中我默认打开表,当我点击它然后它就关闭了。但我想要的是默认情况下必须关闭表格,当我点击它时,表格必须打开???

任何帮助都会受到赞赏吗?

jQuery(function($) {
  $("#polls").on("click", ".flip", function() {
    $(this)
      .closest('tbody')
      .next('.section')
      .toggle('fast');
  }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="polls" style=" border: 1px solid #ccc;">
  <table id="main_table" style="width: 1002px; border: 1px solid #ccc;">
    <tbody>
      <tr style="background-color:green; color:white">
        <td  colspan="" class="flip"> Section 1</td>
        <td  colspan="" class="flip"> Section 2 </td>
        <td  colspan="" class="flip"> Section 3 </td>
        <td  colspan="" class="flip"> Section 4 </td>
      </tr>
    </tbody>
    <tbody 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>
    </tbody>
    <tbody>
      <tr style="background-color:green; color:white">
        <td  colspan="" class="flip"> Section 1 </td>
        <td  colspan="" class="flip"> Section 2 </td>
        <td  colspan="" class="flip"> Section 3 </td>
        <td  colspan="" class="flip"> Section 4 </td>
      </tr>
    </tbody>
    <tbody 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>
    </tbody>
    <tbody>
      <tr style="background-color:green; color:white">
        <td  colspan="" class="flip"> Section 1 </td>
        <td  colspan="" class="flip"> Section 2 </td>
        <td  colspan="" class="flip"> Section 3 </td>
        <td  colspan="" class="flip"> Section 4 </td>
      </tr>
    </tbody>
    <tbody 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>
    </tbody>
  </table>
</div>

5 个答案:

答案 0 :(得分:2)

添加

.section {
    display: none
}
在你的CSS中

答案 1 :(得分:0)

尝试

$(".section").hide();
  jQuery(function($) {
    $("#polls").on("click", ".flip", function() {
      $(this)
        .closest('tbody')
        .next('.section')
        .toggle('fast');
    });
  });
});

DEMO

答案 2 :(得分:0)

这可能有所帮助,因为它似乎是一个相当简单的指南,让我知道。

http://www.jankoatwarpspeed.com/expand-table-rows-with-jquery-jexpand-plugin/

答案 3 :(得分:0)

我更新了代码。请立即查看

$('#main_table').find('.section').hide();

$('.flip').click(function() {
  $(this)
    .closest('tbody')
    .next('.section')
    .toggle('fast');
});

答案 4 :(得分:0)

在你的jQuery(函数($)代码之前添加这个:

  $(".section").hide();