jQuery追加顺序? [英] jQuery Append order?

查看:22
本文介绍了jQuery追加顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一系列评论,并在评论下方回复评论.我使用此代码将新回复附加到评论下方.

$(document).on('click', '.sub-reply1-send', function() {var 回复 = $('textarea[name=comment-reply]').val();$('.sub-reply1,.sub-reply1-textarea,.sub-reply1-send').slideUp(250).remove();$('.answer').append("<div class='comment-sub-reply' style='display:none'>" + reply + "</div>");$('.comment-sub-reply').slideDown(250);subreply_visible = false;});

如果只有一条评论要回复,这可以正常工作,但如果有两条评论,比如评论 1 和评论 2,评论 1 将在评论 2 下方生成.回复 Comment1 可以正常工作,但回复 Comment2 会在 Comment1 的最后一个下方生成回复框.

有什么办法可以让我把它附加到点击它的评论中吗?

根据我拥有的内容生成的 HTML 标记.

<div class="question" style="display: block;"><div class="question-content">这个部分有什么意义?</div>

<div class="answer" style="display: block;"><div class="question-answer" style="" id="thread1">这不是测试<img class="reply" src="reply.png"/>

<div class="question-answer" style="" id="thread0">测试<img class="reply" src="reply.png"/>

<div class="comment-sub-reply" style="">另一个测试</div><div class="comment-sub-reply" style="">还在测试下面</div><div class="comment-sub-reply" style="">这也不是测试,但它正在测试中</div><div class="sub-reply1" style=""><textarea class="sub-reply1-textarea" name="comment-reply" style=""></textarea><div class="sub-reply1-send" style=""></div>

<div id="评论"><表单名称="commentForm"><textarea type="text" name="comment" id="answer-text-input" align="top" class="flexcroll" style="display: block;"></textarea></表单><div id="button" style="display: block;"></div>

JQuery 的其他方面不影响它,只有这一个函数会创建子注释.到目前为止,唯一的线程 ID 没有做任何事情,但我正在努力使这些工作分开评论.

解决方案

使用 jquery .after().使用它,您可以在为 .after() 函数提供的元素之后添加一个新元素.

所以可能做 $('.answer').append() 你应该做 $(this).after().这里 this 将指向被点击的元素

I'm creating a series of comments, with replies to the comments below them. I use this code to append the new reply underneath the comment.

$(document).on('click', '.sub-reply1-send', function() {
    var reply = $('textarea[name=comment-reply]').val();
    $('.sub-reply1, .sub-reply1-textarea, .sub-reply1-send').slideUp(250).remove();
    $('.answer').append("<div class='comment-sub-reply' style='display:none'>" + reply + "</div>");
    $('.comment-sub-reply').slideDown(250);
    subreply_visible = false;
});

This works fine if there's only one comment to reply to, but if there's two comments, let's say Comment1 and Comment2, Comment1 will be generated underneath Comment2. Replying to Comment1 will work fine, but replying to Comment2 would generate the reply-box underneath the last one in Comment1.

Is there any way for me to make it so that it appends to the comment that it's clicked from?

EDIT: HTML markup generated from what I have.

<div id="right-bar" class="flexcroll" style="display: block;">
    <div class="question" style="display: block;">
        <div class="question-content">What is the significance of this section?</div>
    </div>
    <div class="answer" style="display: block;">
        <div class="question-answer" style="" id="thread1">This is not a test
            <img class="reply" src="reply.png" />
        </div>
        <div class="question-answer" style="" id="thread0">Test
            <img class="reply" src="reply.png" />
        </div>
        <div class="comment-sub-reply" style="">Another Test</div>
        <div class="comment-sub-reply" style="">Still underneath Test</div>
        <div class="comment-sub-reply" style="">This isn't a test either, but it's appearing under test</div>
        <div class="sub-reply1" style="">
            <textarea class="sub-reply1-textarea" name="comment-reply" style=""></textarea>
            <div class="sub-reply1-send" style=""></div>
        </div>
    </div>
    <div id="comment">
        <form name="commentForm">
            <textarea type="text" name="comment" id="answer-text-input" align="top" class="flexcroll" style="display: block;"></textarea>
        </form>
        <div id="button" style="display: block;"></div>
    </div>
</div>

The other aspects of the JQuery don't affect it, only this one function creates sub-comments. So far, the unique thread IDs don't do anything, but I'm trying to make that work to seperate the comments.

解决方案

use jquery .after(). using this you can add a new element after the one you provide for the .after() function.

so probably doing $('.answer').append() you should be doing $(this).after(). Here this will point to the element been clicked

这篇关于jQuery追加顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆