jQuery显示前两个回复,隐藏其余部分

时间:2014-06-09 20:17:01

标签: javascript jquery toggle

评论有三个层次。祖父母(深度-1)评论全部显示。我想向每个祖父母评论显示前两个深度2回复(父类评论),并让其余部分隐藏并显示切换按钮(理想情况下,在“隐藏”和“显示”之间更改文本)。除非只有一个深度为2的回复,否则深度为3的回复都应该被隐藏,如果可能的话,应该显示第一个深度为3的回复,以便在所有场景中显示总共两个回复。

我设法在fiddle

中切换所有深度3回复

这是标记和javascript(更新):

<div id="comments">
  <ol class="commentlist">
    <li class="comment byuser comment-author-admin even thread-even depth-1" id="li-comment-677">
      <div id="comment-677" class="grandparent">
        <div class="comment-inner">comment-677
        </div>
        <ul class="children">
            <li class="comment byuser comment-author-admin odd alt depth-2" id="li-comment-678">
                <div id="comment-678" class="parent">
                    <div class="comment-inner">comment-678</div>
                    <ul class="children">
                        <li class="comment byuser comment-author-admin even depth-3" id="li-comment-680">
                            <div id="comment-680">
                                <div class="comment-inner">comment-680</div>
                            </div>
                        </li>
                        <li class="comment byuser comment-author-admin odd alt depth-3" id="li-comment-686">
                            <div id="comment-686">
                                <div class="comment-inner">comment-686</div>
                            </div>
                        </li>
                        <li class="comment byuser comment-author-admin even depth-3" id="li-comment-688">
                            <div id="comment-688">
                                <div class="comment-inner">comment-688</div>
                            </div>
                        </li>
                        <li class="comment byuser comment-author-admin odd alt depth-3" id="li-comment-689">
                            <div id="comment-689">
                                <div class="comment-inner">comment-689</div>
                            </div>
                        </li>
                    </ul>
                </div>
            </li>
         </ul>
      </div>
    </li>   

    <li class="comment byuser comment-author-admin odd alt thread-odd thread-alt depth-1" id="li-comment-679">
      <div id="comment-679" class="grandparent">
        <div class="comment-inner">comment-679
        </div>
        <ul class="children">
            <li class="comment byuser comment-author-admin even depth-2" id="li-comment-682">
              <div id="comment-682" class="parent">
                  <div class="comment-inner">comment-682</div>
                <ul class="children">
                   <li class="comment byuser comment-author-admin odd alt depth-3" id="li-comment-690">
                      <div id="comment-690">
                          <div class="comment-inner">comment-690</div>
                      </div>
                   </li>
                </ul>
               </div>
            </li>

            <li class="comment byuser comment-author-admin even depth-2" id="li-comment-691">
                <div id="comment-691" class="parent">
                  <div class="comment-inner">comment-691</div>
                </div>
            </li>
        </ul>
      </div>
    </li>           

   <ol>
</div>

1 个答案:

答案 0 :(得分:1)

你基本上和第三级做的一样,但使用gt()(从零开始)选择器跳过前两个回复:

http://jsfiddle.net/isherwood/Frpmj/3

$(this).next('ul').find('li:gt(1)').slideToggle();

http://api.jquery.com/gt-selector/