0

我有一个有 5 个部分的手风琴。每个部分里面都有一张桌子。如果该表的行数超过 10 行,则会附加查看更多链接。我的问题是 View More 链接在第一个表中附加了 5 次。4次在第二桌。3次进入第三桌。等等...

如何在超过 10(或 2)行的每个表上仅附加一次“查看更多”链接。

$(".accordion-group table tbody").each(function() {

    var row = $(this).find("tr");
    var colunm = $(this).find("tr:first td");

    row.last().addClass("last");

    var rowCount = row.length;
    var columnCount = colunm.length;

    if (rowCount > 10) {
        $("<tr><td colspan=" + columnCount + " class=\"view_more_accordian\" style=\"text-align:center;\">View more</td></tr>").insertAfter(".last");
    }
}); });

提前感谢您的帮助。

4

1 回答 1

1

原因是在您提供的 insertAfter 选择器中tbody,它将返回所有这些并在所有可用的 tbody 之后插入,而不是将其附加到当前的 tbody 即this

改变

$("<tfoot><tr><td colspan=" + columnCount + " class=\"view_more_accordian\" style=\"text-align:center;\">View more</td></tr></tfoot>")
  .insertAfter("tbody");

$("<tfoot><tr><td colspan=" + columnCount + " class=\"view_more_accordian\" style=\"text-align:center;\">View more</td></tr></tfoot>")
  .insertAfter(this);

您也可以避免使用第一个.each(基于您显示的代码),而是您可以这样做

$(".accordion-group table tbody").each(function () {
...
});
于 2013-12-05T22:08:05.040 回答