如何将新div添加到具有相同类名的最后一个div?

时间:2012-07-03 13:16:27

标签: php jquery

我的所有div块都使用相同的类名,我需要在文件上传完成后附加一个新的div块。我现在这样做的方法是为每个现有的块添加一个新块。这是我的测试代码:

HTML:

<div class="img_container" id="img1">EXISTING BLOCK</div>'
<div class="img_container" id="img2">EXISTING BLOCK</div>'

JQuery的:

$('#fileupload').bind('fileuploadcompleted', function (e, data) {
    e.preventDefault();
    var $newdiv = $('<div class="img_container" id="img3">NEW BLOCK</div>');

    $('.img_container').append($newdiv);
})

我还试图附加到body标签,但新的div从未出现过。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:5)

$('.img_container:last').after($newdiv) // should do the trick