简体   繁体   中英

how to write jQuery for expand/collapse of rows?

This is my html code: http://jsfiddle.net/Udxyb/406/

In the above jQuery code am getting the tables opened by default and when I click on it then it is closing. but what I want is by default the tables must be closed and when I click it then the tables must open???

Any help would be appreciated?

 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> 

Just add

.section {
    display: none
}

in your CSS.

Try

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

DEMO

This may help as it seems to be a rather simple guide, let me know.

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

I updated the code. Please check now

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM