简体   繁体   English

如何逐行使用 jQuery

[英]How can I use jQuery line by line

I want to display comments line by line in a div but the div is closing first and after that the comments are listing.我想在 div 中逐行显示评论,但 div 先关闭,然后再列出评论。

if (comments.num_replies > 0) {
    $("#showComment").append(`<div id="replyContainer${comments.comment_id}" class="replyContainer">`);

    comments.comment_replies.forEach((replies) => {
        $("#showComment").append(`
            <div class="img-comment reply">
                <img src="${home_url}img/user.png">
                <div class="m-2 p-3 ">

                    <div class="user-date"><p class="username">@${replies.replier_username}</p><p class="date">${replies.dates}</p></div>
                    <p class="comment_content">${replies.reply_comment_content.replace(/\n/g, "<br>")}</p>
                </div>
            </div>
        `)
    });

    $("#showComment").append(`</div>`);
}

You can try something like this你可以尝试这样的事情

if (comments.num_replies > 0) {
    let replies = comments.comment_replies.map((replies) => (
        `<div class="img-comment reply">
            <img src="${home_url}img/user.png">
            <div class="m-2 p-3 ">

                <div class="user-date"><p class="username">@${replies.replier_username}</p><p class="date">${replies.dates}</p></div>
                <p class="comment_content">${replies.reply_comment_content.replace(/\n/g, "<br>")}</p>
            </div>
        </div>`
    ));

    $("#showComment").append(`<div id="replyContainer${comments.comment_id}" class="replyContainer">${replies.join('')}</div>`);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM