jQuery附加订单?

我正在创建一系列评论,并回复下面的评论。 我使用此代码在评论下面附加新回复。

$(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("
" + reply + "
"); $('.comment-sub-reply').slideDown(250); subreply_visible = false; });

如果只有一个注释要回复,这样可以正常工作,但是如果有两个注释,那么让我们说Comment1和Comment2,将在Comment2下面生成Comment1。 回复Comment1将正常工作,但回复Comment2将生成Comment1中最后一个下面的回复框。

有没有办法让我这样做,以便它附加到点击它的评论?

编辑:从我拥有的HTML标记生成。

 
What is the significance of this section?
This is not a test
Test
Another Test
Still underneath Test
This isn't a test either, but it's appearing under test

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

使用jquery .after() 。 使用此方法,您可以在为.after()函数提供的元素之后添加新元素。

所以可能正在做$(’。answer’)。append()你应该做$(this).after()。 this将指向已单击的元素

您可以尝试以下方法:

 $(document).on('click', '.sub-reply1-send', function() { // Get the current answer we're commenting on // closest() walks up the DOM and gets us the first parent matching the selector var $answer= $( this ).closest('.question-answer'), reply = $('textarea[name=comment-reply]', $answer).val(); $('.sub-reply1, .sub-reply1-textarea, .sub-reply1-send', $answer).slideUp(250).remove(); $("").appendTo( $answer ).slideDown(250); subreply_visible = false; }); 

请注意,当您在sub-reply1字段上调用remove()时,实际上是从DOM中删除它们。 如果你想在之后的其他地方放置它们,你需要重新创建它们。 你可能已经意识到这一点,但我只是指出来了。

很难理解你在问什么,但你应该有这样的代码来创建你的sub-reply1-send:

 $(document).on('click','.reply',function(){ $('.sub-reply1').remove(); // Remove any previous reply boxes first $(this).parent().after('
'); }

然后将评论置于同一问题答案下:

 $(document).on('click', '.sub-reply1-send', function() { var reply = $('textarea[name=comment-reply]').val(); $(this).parent().after(""); $('.sub-reply1').slideUp(250).remove(); $('.comment-sub-reply').slideDown(250); subreply_visible = false; }); 

或者因为页面上只应该有1个子回复1,所以请在之后加上。

 $(document).on('click', '.sub-reply1-send', function() { var reply = $('textarea[name=comment-reply]').val(); $('.sub-reply1') .after("") .slideUp(250) .remove(); $('.comment-sub-reply').slideDown(250); subreply_visible = false; });