【问题标题】:Toggle some elements to show in jQuery切换一些元素以在 jQuery 中显示
【发布时间】:2014-06-10 16:31:29
【问题描述】:

我希望将元素切换为隐藏(加载时),然后将它们切换为显示。

我有一个 fiddle 显示效果,但是当所有元素都可见然后切换时,除了两个之外的所有元素都被隐藏了。如果可能,所有depth-1(顶级)cmets 都应在页面加载时显示两个回复。最后一条评论 - comment-687 - 应该与它的父 comment-682 一起可见,但我还没有弄清楚如何在所有场景中将两个 cmets 作为默认值。

这里是html

 <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>
                <li class="comment byuser comment-author-admin even depth-2" id="li-comment-698">
                    <div id="comment-698" class="parent">
                        <div class="comment-inner">comment-698</div>
                    </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>
                <li class="comment byuser comment-author-admin odd alt depth-2" id="li-comment-692">
                    <div id="comment-692" class="parent">
                        <div class="comment-inner">comment-692</div>
                    </div>
                </li>
            </ul>
        </div>
    </li>

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

Javascript:

$('ol').find('> li > div > ul')
.before('<div class="toggle">Show all replies</div>');

$('.commentlist').find('div.toggle').click(function () {
    $(this).next('ul').find('.depth-2:gt(1)').slideToggle(); 
    $(this).next('ul').find('.depth-3').slideToggle();
});

【问题讨论】:

  • @Jay Blanchard 谢谢。更正确的是depth-1 类 - 它是顶级评论(祖父母)。我更新了我的问题以使其更清楚。

标签: javascript jquery show-hide slidetoggle


【解决方案1】:

这需要一些切片和切块,但这应该适合你 - http://jsfiddle.net/jayblanchard/Q7kPE/1/

$('ol').find('> li > div > ul')
    .before('<div class="toggle">Show all replies</div>');

/* added this function to hide all but 2 of the d3's where appropriate */
$('.depth-1').each(function() {
    var d3Length = $(this).find('.depth-3').length;
    if(d3Length > 1) { // only want this to affect those with 2 or more d3's
        var $d3 = $(this).find('.depth-3');
        $d3.hide().slice(0, 2).show(); // hide all and then show the first two
    }
});

$('.commentlist').find('div.toggle').click(function () {
    $(this).next('ul').find('.depth-2:gt(1)').slideToggle();
    /* re-factored the code here to take into account the number of d3's */
    var d3Length = $(this).next('ul').find('.depth-3').length; 
    if(d3Length > 1) { // affect only those with 2 or more d3's       
        $(this).next('ul').find('.depth-3:gt(1)').slideToggle(); // slide toggle all of the remaining d3's
    } else {
        $(this).next('ul').find('.depth-3').slideToggle();
    }
});

【讨论】:

  • @Jay Blanchard 非常感谢!你的回答对我很有帮助。我更新了你的小提琴,现在我几乎得到了它。在我说明的所有情况下,将两个回复显示为默认值仍然存在一个小问题 [link]jsfiddle.net/cAdu3 - 请参阅列表中的 3 和 5。
  • 我已经在原始小提琴中工作,以及您对列表的其他要求。您可能需要为这些设置一个案例,就像我之前所做的那样。
  • @Jay Blanchard 抱歉,这是正确的小提琴:[link]jsfiddle.net/cAdu3/1
  • 您在设置变量时遇到了一些错误if(d2Length = 1)。这总是评估为“真”。
  • @Jay Blanchard 是的,我注意到,代码肯定有更多错误,但它在大多数情况下都能正常工作。我需要多学习一些,我是 javascript 的初学者。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
  • 1970-01-01
  • 2021-10-02
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多