5

我尝试.slideToggle("slow");按照此处的定义向我的表添加功能。W3学校

切换效果工作但正常。我需要像上面的 w3school 链接一样的效果。你可以在JSfiddle看到我的代码

是否有任何排序方法(表格扩展器)而不使用任何插件?

这是我的 HTML 表格代码

 <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tablestyle" style="overflow-x: scroll; ">
            <tr>
              <td class="tableviewtop"><input name="" type="checkbox" value=""></td>
              <td class="tableviewtop textaligncenter">Material Name</td>
              <td class="tableviewtop textaligncenter"></td>
              <td class="tableviewtop textaligncenter">Color</td>
              <td class="tableviewtop textaligncenter">Base Price</td>
              <td class="tableviewtop textaligncenter">MSRP</td>
              <td class="tableviewtop textaligncenter">MAP</td>
              <td class="tableviewtop textaligncenter">Net Price</td>
              <td class="tableviewtop textaligncenter">Units Ord.</td>
              <td class="tableviewtop textaligncenter">Total Base Price</td>
              <td class="tableviewtop textaligncenter">Total Net Price</td>
              <td class="tableviewtop textaligncenter">Dis %</td>
            </tr>
            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tda"/></td>
              <td class="textaligncenter">$55.00</td>
              <td class="textaligncenter">$110.00</td>
              <td class="textaligncenter">$110.00</td>
              <td class="textaligncenter">$110.00</td>
              <td class="textaligncenter">140</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "tra1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "tra2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tdb"/></td>
              <td class="textaligncenter">$35.00</td>
              <td class="textaligncenter">$70.00</td>
              <td class="textaligncenter">$70.00</td>
              <td class="textaligncenter">$70.00</td>
              <td class="textaligncenter">120</td>
              <td class="textaligncenter">$3548.00</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">5%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "trb1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "trb2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tdc"/></td>
              <td class="textaligncenter">$60.00</td>
              <td class="textaligncenter">$120.00</td>
              <td class="textaligncenter">$120.00</td>
              <td class="textaligncenter">$120.00</td>
              <td class="textaligncenter">210</td>
              <td class="textaligncenter">$2582.00</td>
              <td class="textaligncenter">$1899.00</td>
              <td class="textaligncenter">30%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "trc1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "trc2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tdd"/></td>
              <td class="textaligncenter">$20.00</td>
              <td class="textaligncenter">$40.00</td>
              <td class="textaligncenter">$40.00</td>
              <td class="textaligncenter">$40.00</td>
              <td class="textaligncenter">155</td>
              <td class="textaligncenter">$6521.00</td>
              <td class="textaligncenter">$6845.00</td>
              <td class="textaligncenter">12%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "trd1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "trd2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tde"/></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">196</td>
              <td class="textaligncenter">$2579.00</td>
              <td class="textaligncenter">$956.00</td>
              <td class="textaligncenter">6%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "tre1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "tre2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tdf"/></td>
              <td class="textaligncenter">$80.00</td>
              <td class="textaligncenter">$160.00</td>
              <td class="textaligncenter">$160.00</td>
              <td class="textaligncenter">$160.00</td>
              <td class="textaligncenter">128</td>
              <td class="textaligncenter">$1899.00</td>
              <td class="textaligncenter">$3548.00</td>
              <td class="textaligncenter">5%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "trf1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "trf2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tdg"/></td>
              <td class="textaligncenter">$30.00</td>
              <td class="textaligncenter">$60.00</td>
              <td class="textaligncenter">$60.00</td>
              <td class="textaligncenter">$60.00</td>
              <td class="textaligncenter">451</td>
              <td class="textaligncenter">$6845.00</td>
              <td class="textaligncenter">$2582.00</td>
              <td class="textaligncenter">30%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "trg1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "trg2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tdh"/></td>
              <td class="textaligncenter">$35.00</td>
              <td class="textaligncenter">$70.00</td>
              <td class="textaligncenter">$70.00</td>
              <td class="textaligncenter">$70.00</td>
              <td class="textaligncenter">618</td>
              <td class="textaligncenter">$956.00</td>
              <td class="textaligncenter">$6521.00</td>
              <td class="textaligncenter">8%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "trh1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "trh2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

            <tr>
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter">Demo Name</td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "http://www.iconfinder.com/icons/14227/download/png/16" style = "padding-left:3px;" id = "tdi"/></td>
              <td class="textaligncenter">$44.00</td>
              <td class="textaligncenter">$88.00</td>
              <td class="textaligncenter">$88.00</td>
              <td class="textaligncenter">$88.00</td>
              <td class="textaligncenter">426</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">$2579.00</td>
              <td class="textaligncenter">15%</td>
            </tr>

            <!-- Expander Data Start -->

            <tr style = "display:none;" id = "tri1">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxred.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">255</td>
              <td class="textaligncenter">$3284.00</td>
              <td class="textaligncenter">$7581.00</td>
              <td class="textaligncenter">15%</td>
            </tr>
            <tr style = "display:none;" id = "tri2">
              <td><input name="" type="checkbox" value=""></td>
              <td class="textaligncenter"></td>
              <td class="textaligncenter"><a href="#"></a></td>
              <td class="textaligncenter"><img src = "images/icon_boxyellow.png" /></td>
              <td class="textaligncenter">$25.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">$50.00</td>
              <td class="textaligncenter">511</td>
              <td class="textaligncenter">$2948.00</td>
              <td class="textaligncenter">$4815.00</td>
              <td class="textaligncenter">10%</td>
            </tr>

            <!-- Expander Data End -->

          </table>

jQuery代码

// Table jquery code start

$("#tda").click(function () {
    $("#tra1").slideToggle("slow");
    $("#tra2").slideToggle("slow");
});
$("#tdb").click(function () {
    $("#trb1").toggle("slow");
    $("#trb2").toggle("slow");
});
$("#tdc").click(function () {
    $("#trc1").toggle("slow");
    $("#trc2").toggle("slow");
});
$("#tdd").click(function () {
    $("#trd1").toggle("slow");
    $("#trd2").toggle("slow");
});
$("#tde").click(function () {
    $("#tre1").toggle("slow");
    $("#tre2").toggle("slow");
});
$("#tdf").click(function () {
    $("#trf1").toggle("slow");
    $("#trf2").toggle("slow");
});
$("#tdg").click(function () {
    $("#trg1").toggle("slow");
    $("#trg2").toggle("slow");
});
$("#tdh").click(function () {
    $("#trh1").toggle("slow");
    $("#trh2").toggle("slow");
});
$("#tdi").click(function () {
    $("#tri1").toggle("slow");
    $("#tri2").toggle("slow");
});

// Table jquery code end
4

3 回答 3

1

更新了您的 jsfiddle 代码,请参阅此链接 > http://jsfiddle.net/d5hWe/31/

$('tr.main').click(function() {
  $(this).nextUntil("tr.main").delay(1000).slideToggle("slow");
});


//To expand all rows for the table... 
$('tr.main').click(function() {
  $(this).nextAll('.child').delay(1000).slideToggle("slow");
});

你的父行现在有一个“主”类,子行有一个“子”类

我添加了 2 个使用 slideToggle 的函数,它们都通过单击父行上的任何位置来工作。单击父行将设置 1 秒延迟,然后该父行的子行将向上/向下切换。

我还添加了一个注释掉的函数,该函数将滑动切换所有子行,无论单击哪个父行,以防万一您需要它。


看看这个答案,它还有一个jsfiddle,它显示了表格行的更平滑的幻灯片。

为表格行实现向下滑动

所有功劳归于 mrtsherman

于 2013-05-13T12:35:40.460 回答
1

Baber 提供了一个指向 mrtsherman 的 jsfiddle 代码的链接。将其修改为:

$('tr').not(':first').children('td').wrapInner('<div>');

$('button').click( function() {
    $('tr').not(':first').slideToggle();
    $('td > div').slideToggle();
});

使其切换。

于 2013-05-13T13:31:15.980 回答
1

对于正在查看实际滑动效果的人,您可以尝试以下操作:

  • 向行添加一个类toToggle以切换

  • 用相同的类将所有<td>内容包装在 a 中:<div>toToggle

    $('.toToggle').children('td').wrapInner('<div class="toToggle"></div>');
    
  • 要有滑动的效果,需要<td><div>需要一起滑动。我们给了两个相同的toToggle类,所以我们可以用一个 jquery 指令来切换两者

    $('.toToggle').toggle(2000);
    

$('.toToggle').children('td').wrapInner('<div class="toToggle"></div>');
$('.toClick').click(function(){
    $('.toToggle').toggle('slow');
});
.toClick {cursor: pointer}
.toClick td {padding: 15px}
.toToggle {padding: 15px}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1">
    <tr class="toClick">
      <td>click me!</td>
      <td>click me!</td>
    </tr>
    <tr class="toToggle">
      <td>text</td>
      <td>text</td>
    </tr>
    <tr class="toToggle">
      <td>text</td>
      <td>text</td>
    </tr>
</table>

于 2014-08-08T14:12:49.733 回答