【问题标题】:Is there anyway to show a hidden div in the last row of a html table无论如何在html表格的最后一行显示一个隐藏的div
【发布时间】:2010-03-24 04:22:03
【问题描述】:

我有一个 html 表格。这是一个简化的版本:

<table>
      <tr>
           <td><div style="display: none;" class="remove0">Remove Me</div></td>
      </tr>
      <tr>
           <td><div style="display: none;" class="remove1">Remove Me</div></td>
      </tr>
      <tr>
           <td><div class="remove2">Remove Me</div></td>
      </tr>
</table>

我有一个 Javascript,它点击最后一行中的 Remove Me 并使用以下命令删除 html 行:

$(this).parents("tr:first").remove();

问题是,当我删除最后一行时,我还希望“删除我”文本现在显示在第二行(现在是新的最后一行)。我将如何显示此 div 以便它从新的最后一行动态显示“删除我”?

【问题讨论】:

    标签: jquery html asp.net-mvc html-table


    【解决方案1】:

    试试这个。为所有 div 赋予相同的类 - 从长远来看会简单得多。

    <table>
      <tr>
           <td><div class="remove">Remove Me</div></td>
      </tr>
      <tr>
           <td><div class="remove">Remove Me</div></td>
      </tr>
      <tr>
           <td><div class="remove">Remove Me</div></td>
      </tr>
    </table>
    

    还有这个javascript:

    $('.remove')
        .click(function() {
            var $tr = $(this).closest('tr');
            $tr
                .prev()
                .find('.remove')
                .show()
            ;
            $tr.remove();
        })
        // hide all but the last one
        .slice(0, -1).hide()
    ;
    

    【讨论】:

    • @oo 另一种方法是只有一个“删除”链接,当您单击它时,它会自行移动到上一行。如果您想知道该怎么做,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2012-07-20
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多