【问题标题】:Jquery- Hiding all tr except the first oneJquery-隐藏除第一个之外的所有 tr
【发布时间】:2013-10-13 22:40:21
【问题描述】:

我想隐藏除第一个之外的所有 tr。然后我希望每个 tr 在有人点击添加链接时出现。

如何隐藏除第一行之外的所有表格行。

这是我的http://jsfiddle.net/Nx4cD/9/

$( document ).ready(function() {
$(".add").click(function() {
    var
    $this = $(this),
    $row = $this.closest("tr"),
    $rowIndex = $row.index();
    $row.next().show("medium").find("td").eq(0).html($rowIndex+2);
});
$(".delete").click(function() {
    $(this).closest('tr').hide();
});

});

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您可以使用.not() 方法排除第一个元素:

    $('table tbody tr').not(':first').hide();
    

    对于选择下一个隐藏的tr 元素,您可以使用.nextAll():hidden 选择器:

    var $row = $this.closest("tr").nextAll(':hidden').first();
    

    【讨论】:

    • 谢谢。 jsfiddle.net/Nx4cD/10 所以,这样可以吗?或者可以优化。我还想为每一行显示 s.no。不想直接用html写。我设法使用 index() 来做到这一点。有没有更好的方法。以及如何在第一行显示 s_no
    • 我还想为每个 tr 显示 s_no。你能帮我吗
    • @user2847429 不客气,理想的方法是创建/删除tr 元素。您可以使用.clone() 方法或使用JavaScript 创建trtd 元素。
    • 好的。为每个 tr 元素显示序列号怎么样?
    • @user2847429 检查这个jsfiddle.net/6rHks,这个工作但我不推荐它,请检查这个问题,stackoverflow.com/questions/2362982/…stackoverflow.com/questions/4789923/…
    【解决方案2】:

    尝试使用JQuery indexhere

    $(".delete").click(function() {
            var parentRow = $(this).closest('.table > tbody > tr');
            if(parentRow.index() != 0){
                parentRow.hide();
            }  
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-30
      • 2012-09-08
      • 2012-05-14
      • 1970-01-01
      • 2011-10-23
      • 2021-12-28
      • 2015-06-18
      • 2022-10-24
      相关资源
      最近更新 更多