【问题标题】:remove last append element jquery删除最后一个附加元素 jquery
【发布时间】:2010-12-11 15:36:09
【问题描述】:

我有以下代码:

$(this).children("a:eq(0)").append('<img src="'+ (arrowsvar.down[1]) 
    +'" class="' + (arrowsvar.down[0])
    + '" style="border:0;" />'
);

现在我想删除最后附加的元素(图像)。请提出建议。

【问题讨论】:

    标签: jquery append


    【解决方案1】:

    作为替代方案,对于那些喜欢更多程序化方式和语法的人:

    $('#container-element img').last().remove();
    

    【讨论】:

      【解决方案2】:

      你可以使用:last-child选择器找到最后附加的元素,然后你可以remove它:

      $('img:last-child', this).remove();
      // get the last img element of the childs of 'this'
      

      【讨论】:

        【解决方案3】:

        原始问题的代码可能会产生错误(在选择器中使用“this”)。尝试以下方法:

        $("#container-element img:last-child").remove()
        

        【讨论】:

        • 如果我们想删除我们上次添加的所有元素??
        • 您可能希望避免使用“上述代码”这样的表达方式,因为这可能会改变。
        【解决方案4】:

        太棒了,非常感谢尤金,这对我很有帮助。我不得不删除一个表,我使用了以下代码。

        $("#mydivId table:last-child").remove()
        

        【讨论】:

        • 这应该是对尤金答案的评论,而不是单独的答案。
        【解决方案5】:

        试试这个。刚刚添加了 .not(':last-child') ,它不包括集合中的最后一个元素。所以你不必删除它。

        $(this).children("a:eq(0)").not(':last-child').append('<img src="'+ (arrowsvar.down[1]) 
            +'" class="' + (arrowsvar.down[0])
            + '" style="border:0;" />'
        );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-12
          • 1970-01-01
          相关资源
          最近更新 更多