在最后一个孩子jQuery之前插入

时间:2018-08-13 23:38:26

标签: javascript jquery

我有一个包含帖子,评论和回复的页面。帖子可以有很多评论,每个评论可以有很多回复-类似于Facebook的工作方式。

我通过ajax调用将这些动态添加到每个帖子。

每个答复列表的末尾都有一个答复表单。我遇到的问题是,将新答复添加到评论时,我无法在答复表单之前插入答复-这是li的最后一个孩子。

这是我的js:

var $div = $('.js-post-show');

function _addComment(comment) {
    var tplText = $('#js-comment-template').html();
    var tpl = _.template(tplText);
    var html = tpl(comment);

    $div.find('.js-comment-list')
        .append($.parseHTML(html));
}

function _addReply(reply, commentId) {
    var tplText = $('#js-reply-template').html();
    var tpl = _.template(tplText);
    var html = tpl(reply);

    $div.find("li[data-comment-id='" + commentId + "']")
        .append($.parseHTML(html));
}

function _loadReplyForm(comment) {
    var tplText = $('#js-reply-form-template').html();
    var tpl = _.template(tplText);
    var html = tpl(comment);

    $div.find("li[data-comment-id='" + comment.id + "']")
        .append($.parseHTML(html));
}

这是我的HTML:

<li class="list-group-item py-4 comment-list" data-comment-id="871">
<div class="media">
    <div class="avatar avatar-sm">JD</div>
    <div class="media-body">
        <div class="mb-2">
            <span class="h6 mb-0">John Doe</span>
        </div>
        <p>
            Comment here...
        </p>
        <div class="d-flex align-items-center">
            <div class="mr-2">
                <button class="btn btn-sm btn-outline-primary" data-target="#create-reply-871" data-toggle="collapse" aria-expanded="true" aria-controls="create-reply-871">Reply</button>

                <button class="btn btn-sm btn-outline-primary js-like-comment" data-url="/api/comments/871/toggle-comment-like" data-is-liked="1">
                    <span class="fas fa-thumbs-up"></span>
                    <span class="js-like-comment-count">1</span>
                </button>
            </div>
        </div>
    </div>
</div>

<div class="media">
    <div class="avatar avatar-sm">JD</div>
    <div class="media-body">
        <div class="mb-2">
            <span class="h6 mb-0">Jane Doe</span>
        </div>
        <p>
            Reply here...
        </p>
        <div class="d-flex align-items-center">
            <div class="mr-2">
                <button class="btn btn-sm btn-outline-primary" data-target="#create-reply-24" data-toggle="collapse" aria-expanded="false" aria-controls="create-reply-24">Reply</button>

                <button class="btn btn-sm btn-outline-primary js-like-reply" data-url="/api/replies/24/toggle-reply-like" data-is-liked="0">
                    <span class="far fa-thumbs-up"></span>
                    <span class="js-like-reply-count">0</span>
                </button>
            </div>
        </div>
    </div>
</div>

<div class="media">
    <div class="avatar avatar-sm">RS</div>
    <div class="media-body">
        <div class="mb-2">
            <span class="h6 mb-0">Rob Smith</span>
        </div>
        <p>
            Another reply
        </p>
        <div class="d-flex align-items-center">
            <div class="mr-2">
                <button class="btn btn-sm btn-outline-primary" data-target="#create-reply-25" data-toggle="collapse" aria-expanded="false" aria-controls="create-reply-25">Reply</button>

                <button class="btn btn-sm btn-outline-primary js-like-reply" data-url="/api/replies/25/toggle-reply-like" data-is-liked="0">
                    <span class="far fa-thumbs-up"></span>
                    <span class="js-like-reply-count">0</span>
                </button>
            </div>
        </div>
    </div>
</div>

<div class="media js-create-reply collapse show" id="create-reply-871" style="">
    <div class="media-body">
        <form method="post" class="card-body js-create-reply-form needs-validation" novalidate="novalidate" data-url="/api/replies/871">
            <div class="form-group"><textarea id="reply" name="reply" required="required" class="from-control-lg js-create-reply-textarea form-control" rows="4" placeholder="Type your reply here"></textarea></div>
            <div class="d-flex align-items-center">
                <button type="submit" class="btn btn-success mr-3 js-create-reply-button">Submit your reply</button>
                <a href="#create-reply" class="text-small text-muted" data-toggle="collapse" data-target="#create-reply-871" aria-expanded="true" aria-controls="create-reply-871">Cancel</a>
            </div>
        </form>
    </div>
</div>
</li>

我尝试了insertBefore(),但没有成功,任何建议将不胜感激。

1 个答案:

答案 0 :(得分:8)

您可以使用insertAfter() jQuery函数:http://api.jquery.com/insertafter/ 您也可以在选择器中使用:last来定位最后一个答复,例如

$(replyHtml).insertAfter("#js-reply-template li:last");