Javascript 如何显示两个回复,并将其余回复折叠到“查看回复”下。(我很难给出两个答复)

Javascript 如何显示两个回复,并将其余回复折叠到“查看回复”下。(我很难给出两个答复),javascript,jquery,Javascript,Jquery,我有一个评论系统,它目前折叠所有的答复。我想显示两个回复,然后将它们全部折叠起来。现在我研究了这个问题,但我不太明白,因为我使用for循环进行回复。这是我的密码 <div class="replyInfo"> {% if not comment.is_child %} <div class="wholeReply"> {% if comment.comment_count %} <a href='#' cl

我有一个评论系统,它目前折叠所有的答复。我想显示两个回复,然后将它们全部折叠起来。现在我研究了这个问题,但我不太明白,因为我使用for循环进行回复。这是我的密码

<div class="replyInfo">
    {% if not comment.is_child %}
        <div class="wholeReply">
          {% if comment.comment_count %}
        <a href='#' class='replies'>           
         {{comment.comment_count}}</a>
          {% endif %}
        <div class="got_replies">
        <ul style="list-style-type: none;">
        {% for child in comment.get_children %}
        <li>
      {{ child.get_comment }}
        </li><hr>
        {% endfor %}
        </ul>
      </div>
    </div>
  </div>

<script>
$('.replies').click(function(e){
  e.preventDefault();
  $(this).next(".got_replies").fadeToggle(); 
})
$(".got_replies").fadeToggle(); 

</script>

{%if not comment.is_child%}
{%if comment.comment_count%}
{%endif%}
    {comment.get_children%}
  • {{child.get_comment}}

  • {%endfor%}
$('.repress')。单击(函数(e){ e、 预防默认值(); $(this).next(“.got_responses”).fadeToggle(); }) $(“.got_responses”).fadeToggle();

有人能帮帮我吗?

目前,你把整个部门都藏起来了。 我没有测试它,但请尝试以下操作:

<script>
$('.replies').click(function(e){
  e.preventDefault();
  $(this).next(".got_replies").children("ul").children("li:nth-child(n+2)").fadeToggle(); 
})

</script>

$('.repress')。单击(函数(e){
e、 预防默认值();
$(this).next(“.got_responses”).children(“ul”).children(“li:nth child(n+2)”).fadeToggle();
})
您可以使用
slice()
函数选择类名为
reply
的前两个元素。当然,您必须将类名
reply
添加到html中

添加了类名的HTML:

<div class="replyInfo">
    {% if not comment.is_child %}
        <div class="wholeReply">
          {% if comment.comment_count %}
        <a href='#' class='replies'>           
         {{comment.comment_count}}</a>
          {% endif %}
        <div class="got_replies">
        <ul style="list-style-type: none;">
        {% for child in comment.get_children %}
        <li class="reply"><!--added class to your li element-->
      {{ child.get_comment }}
        </li><hr>
        {% endfor %}
        </ul>
      </div>
    </div>
  </div>
    $('.replies').click(function(e){
  e.preventDefault();
  $(this).next(".got_replies").find(".reply").slice(0,2).fadeToggle(); 
});

$(".reply").fadeToggle();

这是用树枝吗?如果是这样的话,你能用它来标记你的问题吗?@trincot你能发布你的呈现html吗?@LarryLane,你认为我是OP吗?是的,我输入了错误的名称,很抱歉。您的代码可以正常工作,但值得注意的是,第n个子选择器没有使用基于零的索引(如JavaScript数组),因此如果您想在第2个li之后切换所有内容,则必须使用n+3。没问题,这是一个简单的修复方法。感谢Larry的解决方案,但我需要注释(不是前两个)要折叠直到单击“查看所有评论”链接。我放了$(“.got_represents”).fadeToggle();最后,但整个js都不起作用,你能给我一些提示吗?@emfour我很乐意帮忙,但请澄清。我以为你想在第二条评论之后隐藏每条评论,直到用户点击按钮?我想做的是,当前每条评论都会显示,当我点击按钮时,只有前两条评论会显示出来。所以你想在点击按钮时只显示前两条评论?是的,先生,你擅长ajax吗?我有这个问题,我快发疯了你介意看一下吗?