Using jQuery to slideToggle a group of Table Rows

孤街醉人 提交于 2019-12-01 04:12:39

You can't mix <div>s into a <table> like that, use additional <tbody> elements instead. In your callback, this is the <td> element which has no siblings so .next does nothing useful; you want to go back up until you get to something that is at the same depth as the .section you're interested in and then call .next from there.

Your HTML should look more like this:

<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>
        <!-- ... -->

And your click handler like this:

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

The .closest call goes back up your ancestors until it finds a <tbody> and then you call .next on that.

Updated jsfiddle: http://jsfiddle.net/ambiguous/Udxyb/4/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!