在最后一个div之后追加项目

时间:2015-11-05 06:56:44

标签: javascript jquery

<div class="follower-list">
              <?php if (!$publications) { ?>
                Henüz paylaşılan bir yayın bulunmamaktadır.
              <?php } else { 
                        for ($i = 0; $i < count($publications); $i++) {         
              ?>
                            <div class="media">
                            <div class="media-body">
                                <div class="col-sm-11">
                                    <i class="fa fa-file-text"></i> <?=$publications[$i]["content"]?>
                                </div>
                                <div class="col-sm-1">
                                    <button class="btn btn-default" onclick="$.delete_publication('<?=$publications[$i]["id"]?>')">Kaldır</button>
                                </div>
                            </div>
                            </div>
                    <?php } 
                    } ?>

              <div class="mb20"></div>

              <button class="btn btn-primary" id="add_new_publication">Yeni Yayın Ekle</button>

              <div class="input-group" id="div_publications" style="display: none;">
                <input class="form-control" type="text" id="input_publication" name="publication" placeholder="Yayın Adı">
                <span class="input-group-btn">
                    <button type="button" class="btn btn-success" onclick="$.save_new_publication(<?=$USER_ID?>);">Kaydet</button>
                    <button type="button" class="btn btn-danger" onclick="$.cancel_new_publication();">İptal</button>
                  </span>
            </div>


            </div><!--follower-list -->

我动态添加了一些文字,但我想在最后一个媒体类

之后添加

这是我的js:

$(".media").append('<div class="media"><div class="media-body"><div class="col-sm-11"><i class="fa fa-file-text"></i> ' + response.content + '</div><div class="col-sm-1"><button class="btn btn-default" onclick="$.delete_publication(' + response.id + ')">Kaldır</button></div></div></div>');

附加工作但它在具有media类的所有div之后附加div。但我想追加media

3 个答案:

答案 0 :(得分:2)

使用 last()

$(".media").last().append('<div class="media"><div class="media-body"><div class="col-sm-11"><i class="fa fa-file-text"></i> ' + response.content + '</div><div class="col-sm-1"><button class="btn btn-default" onclick="$.delete_publication(' + response.id + ')">Kaldır</button></div></div></div>');

<小时/> 或 :last

$(".media:last").append('<div class="media"><div class="media-body"><div class="col-sm-11"><i class="fa fa-file-text"></i> ' + response.content + '</div><div class="col-sm-1"><button class="btn btn-default" onclick="$.delete_publication(' + response.id + ')">Kaldır</button></div></div></div>');

答案 1 :(得分:0)

使用:last选择器

$(".media:last").append();

答案 2 :(得分:0)

Try this
$(".media:last").append('<div class="media"><div class="media-body"><div class="col-sm-11"><i class="fa fa-file-text"></i> ' + response.content + '</div><div class="col-sm-1"><button class="btn btn-default" onclick="$.delete_publication(' + response.id + ')">Kaldır</button></div></div></div>');