简体   繁体   English

jQuery切换在一定数量的匹配元素之后显示/隐藏元素

[英]jQuery toggle show/hide elements after certain number of matching elements

In implementing faceted search, if the number of options is 7 or less, I will show them all. 在实施分面搜索时,如果选项数量为7或更少,我将全部显示。 If the number of options exceed 7, I will show the first 5 only and insert a link that will toggle the display of these options. 如果选项数超过7,我将仅显示前5个,并插入一个链接以切换这些选项的显示。

My question in this case is, how to run through the list of matching elements, in this case li 's that fall within .facet ul , and perform this function. 我在这种情况下的问题是,如何运行匹配元素列表,在这种情况下, li属于.facet ul ,并执行此功能。 Secondly, I need to .appendTo() an li at the end of .facet ul that displays text based on whether or not I am showing all or some. 其次,我需要.appendTo().facet ul结尾处的li ,根据我是否显示全部或部分来显示文本。

If showing all, I want the text to read "... Fewer Choices". 如果全部显示,我希望文本显示为“......更少的选择”。 If I am showing few I would like the text to read "... n More Choices". 如果我出一些我想读课文“...... N多的选择。”

A push in the right direction for each of these functions, or a complete explanation is much appreciated. 为这些功能中的每一个推进正确的方向,或者非常感谢完整的解释。

Code below for reference. 以下代码供参考。

    <div class="facet">
  <h4>Brands</h4>
  <ul>
   <li><a class="all" href="#">Really long brand name facet to show what happens with multi-line facets <em>(63)</em></a></li>
   <li><a class="all" href="#">Joe Rocket <em>(57)</em></a></li>
   <li><a class="all" href="#">Icon <em>(42)</em></a></li>
   <li><a class="all" href="#">Fieldsheer <em>(37)</em></a></li>
   <li><a class="all" href="#">Tour Master <em>(21)</em></a></li>
   <li><a class="all" href="#">AGV Sport<em>(21)</em></a></li>
   <li><a class="all" href="#">Alpinestars<em>(21)</em></a></li>
   <li><a class="all" href="#">Cortech<em>(21)</em></a></li>
   <li><a class="all" href="#">Element<em>(21)</em></a></li>
   <li><a class="all" href="#">Fieldsheer<em>(21)</em></a></li>
   <li><a class="all" href="#">Firstgear<em>(21)</em></a></li>
   <li><a class="all" href="#">FMF Apparel<em>(21)</em></a></li>
   <li><a class="all" href="#">Icon<em>(21)</em></a></li>
   <li><a class="all" href="#">Joe Rocket<em>(21)</em></a></li>
   <li><a class="all" href="#">O'Neal Racing<em>(21)</em></a></li>
   <li><a class="all" href="#">Power Trip<em>(21)</em></a></li>
   <li><a class="all" href="#">REV'IT!<em>(21)</em></a></li>
   <li><a class="all" href="#">River Road<em>(21)</em></a></li>
   <li><a class="all" href="#">Rockstar<em>(21)</em></a></li>
   <li><a class="all" href="#">Scorpion<em>(21)</em></a></li>
   <li><a class="all" href="#">Shift Racing<em>(21)</em></a></li>
   <li><a class="all" href="#">Speed and Strength<em>(21)</em></a></li>
   <li><a class="all" href="#">Spidi<em>(21)</em></a></li>
   <li><a class="all" href="#">Teknic<em>(21)</em></a></li>
   <li><a class="all" href="#">Tour Master<em>(21)</em></a></li>
   <li><a class="all" href="#">Troy Lee Designs<em>(21)</em></a></li>
   <li><a class="all" href="#">Vega<em>(21)</em></a></li>
   <li><a class="all" href="#">Yoshimura<em>(21)</em></a></li>
   <li><a class="all" href="#">Z1R<em>(21)</em></a></li>
  </ul>
 </div>

The 'all' class is irrelevant here and serves another purpose. “全部”课程在这里无关紧要,有另一个目的。 Feel free to disregard it. 随意忽略它。

You're looking for the :gt selector: 您正在寻找:gt选择器:

$('.facet').each(function() {
    var ul = $('ul', this);
    if(ul.children('li').size() <= 7) return;

    var hiddenElements = ul.children('li:gt(4)', this).hide();

    var showCaption = '...' + hiddenElements.size() + ' More Choices';
    ul.append(
        $('<li class="toggler">' + showCaption + '</li>')
            .toggle(
                function() { 
                    hiddenElements.show();
                    $(this).text('...Fewer Choices');
                }, 
                function() { 
                    hiddenElements.hide();
                    $(this).text(showCaption);
                }
            )
    );
});

DEMO DEMO

Here is a start: 这是一个开始:

$(document).ready(
  function(){

    var count=0;
    $('div.facet ul li').each(
      function(){
        if(++count == 7){
          $(this).parent().append('<li><a href="">Click here for more...</a></li>');
        }
        else if(count > 7){
          $(this).css('display','none');
        }
      }
    );

  }
);

my two cents 我的两分钱

$('.facet li').each(function(x) {
    var warn = ''
    if($('.facet li').length > 7){
        if (x >= 5){
            $(this).hide(); 
        }
        warn = '.. Fewer Choices';
    }else{
        $(this).show(); 
        warn = 'test to append if less than 7 than seven';
    }
    if ($('#warn').length == 0){
                    $('.facet ul').append('<li id="warn"></li>');
    }
    $('#warn').text(warn);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM