简体   繁体   English

如何在第 n 个项目之后隐藏和显示直接子列表项目

[英]How can hide and show of direct child list items after nth items

Say I have an unordered list, like so:假设我有一个无序列表,如下所示:

Demo演示

HTML Code: HTML代码:

<ul class="nav_accordian">
  <li>One
  <ul>
  <li>on 1.1</li>
  <li>on 1.2</li>
  <li>on 1.3</li>
  </ul>
  </li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>One 1</li>
  <li>Two 2</li>
  <li>Three 3</li>
  <li>Four 4</li>
  <li>Five 5</li>
  <li>One 11</li>
  <li>Two 22</li>
  <li>Three 33</li>
  <li>Four 44</li>
  <li>Five 55</li>
</ul>

Script File :脚本文件:

$('.nav_accordian').each(function(){
    var max = 4
    if ($(this).find('li').length > max) {
        $(this).find('li:gt('+max+')').hide().end().append('<li class="sub_accordian"><span class="showMore">show More</span></li>');

        $('.sub_accordian').click( function(){
            $(this).siblings(':gt('+max+')').toggle();

            if ( $('.show_more').length ) {
                $(this).html('<span class="showMore">show More</span>');
            } 
            else {
                $(this).html('<span class="showMore">less More</span>');
            };
        });
    };
});

i did for when i click on 'show more' it should be visible all the results of list items .我这样做是因为当我点击“显示更多”时,它应该可以看到列表项的所有结果。 Once every results loaded 'show more' text would be changed on 'less more'.加载每个结果后,“显示更多”文本将更改为“更少”。

and when i clicked upon the 'less more' button first five results result items should be visible other items would be hide.当我点击“更少”按钮时,前五个结果结果项应该是可见的,其他项将被隐藏。

The issue is the script is consider only direct child list of i did for when i click on 'show more' it should be visible next five results of list items .问题是当我点击“显示更多”时,脚本只考虑我所做的直接子列表,它应该在列表项的下五个结果中可见。 Once every results loaded 'show more' text would be changed on 'less more'.加载每个结果后,“显示更多”文本将更改为“更少”。

and when i clicked upon the 'less more' button first results result items should be visible other items would be hide.当我点击“更少”按钮时,第一个结果结果项目应该是可见的,其他项目将被隐藏。

but my script is consider li child ul also but i dont want但我的剧本也考虑 li child ul 但我不想要

i am expected output as:我的预期输出为:

one

two

three

four

five

Show more展示更多

Try to use the > child selector whenever you want to target the first level children.每当您想要定位第一级子级时,请尝试使用>子级选择器。

if ($(this).find('> li').length > max) {
//----------------^
        $(this).find('> li:gt('+max+')').hide().end().append('<li ....
//--------------------^

DEMO演示

From your explanation I get confused... but as a contribution, this is the code I did... HTML:从你的解释中我感到困惑......但作为贡献,这是我所做的代码...... HTML:

<ul class="nav_accordian">
 <li>One
  <ul>
   <li>on 1.1</li>
   <li>on 1.2</li>
   <li>on 1.3</li>
  </ul>
 </li>
<li>Two
 <ul>
   <li>on 2.1</li>
   <li>on 2.2</li>
   <li>on 2.3</li>
  </ul>
 </li>
 <li>Three
  <ul>
   <li>on 3.1</li>
   <li>on 3.2</li>
   <li>on 3.3</li>
  </ul>
 </li>
</ul>

*Sorry about the indentation. *抱歉缩进。

jQuery: jQuery:

$('.nav_accordian > li').each(function(){
  $(this).children('ul').hide();
  $(this).append('<li class="sub_accordian"><span class="showMore">show More</span></li>');//.children().hide();
}); 

$(document).on('click','.sub_accordian', function(){
  $(this).siblings().toggle();
  if ( $('.show_more').length ) {
    $(this).html('<span class="showMore">show More</span>');
  } 
  else {
    $(this).html('<span class="showMore">less More</span>');
  }
});

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

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