如何使用 Jquery 截断长无序列表?

发布于 2024-12-21 18:32:24 字数 664 浏览 3 评论 0原文

我正在使用 wordpress 显示类别列表,按字母顺序排序。我想使用 jQuery 在前 20 个链接后面附加一个“阅读更多”链接。当用户单击“阅读更多”时,其余类别会向下滑动/显示。列表的 HTML 格式如下:

<ul id="catA">
    <h2>Categories</h2>
    <li class="cat-item">
        <a title="title" href="#">Category Name</a>
    </li>
    <li class="cat-item">
        <a title="title" href="#">Category Name</a>
    </li>
    <li class="cat-item">
        <a title="title" href="#">Category Name</a>
    </li>
    ...
</ul>

我希望将 查看所有类别 添加到 20rh 类别之后的列表中,并隐藏所有内容在那之后。在我尝试编写一些代码之前,对解决此问题的最佳方法有什么想法吗?

I am using wordpress to display a list of categories, sorted alphabetically. I want to use jQuery to append a "read more" link after the first 20 links. When the user clicks "read more" the rest of the categories slide down/appear. The HTML format of the list is below:

<ul id="catA">
    <h2>Categories</h2>
    <li class="cat-item">
        <a title="title" href="#">Category Name</a>
    </li>
    <li class="cat-item">
        <a title="title" href="#">Category Name</a>
    </li>
    <li class="cat-item">
        <a title="title" href="#">Category Name</a>
    </li>
    ...
</ul>

I want the <a href="#">view all categories</a> to be appended to the list after the 20rh category, and hiding everything after that. Before I attempt to write some code, any thoughts on the best way to go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月苍老的讽刺 2024-12-28 18:32:24

我会使用 :eq() 选择器找到第 20 个 li 元素(注意它是 < em>零索引),在其后添加“查看全部”链接,然后使用 :gt() 选择器 选择并隐藏你多余的 li 元素。您需要将一个事件处理程序附加到“查看全部”链接,以显示隐藏的 li 并自行删除。

I would find the 20th li element using the :eq() selector (noting that it is zero-indexed), add your View All link after it, then make use of the :gt() selector to select and hide your extra li elements. You would need to attach an event handler to your View All link that shows the hidden li's and removes itself.

℡寂寞咖啡 2024-12-28 18:32:24

我会这样做:

$('ul#catA').children('li').each(function (i)
{
    if (i > 19)
    {
        $(this).hide();   
    }
});

var more = $('<li class="cat-item"><a href="">Show More</a></li>');
$('ul#catA').append(more);

more.find('a').on('click', function (){
   $(this).parent().siblings('li').show();

   return false; 
});

这是 jsfiddle 显示我的方法的实际应用。


我根据 @nachito 关于使用 :gt()jsfiddle代码>选择器。我还添加了一些评论来解释正在发生的事情。

// creating the new 'Show More' link
var more = $('<li class="cat-item"><a href="">Show More</a></li>');

// hiding all lis after the first two
$('ul#catA').children('li:gt(1)').hide();

// adding the 'Show More' link
$('ul#catA').append(more);

// binding a click event to 'Show More'
more.find('a').on('click', function (){        
   // hiding 'Show More' and showing the rest of the lis in this ul 
   $(this).parent().hide().siblings('li').show();

   // preventing default action and event bubbling
   return false; 
});

I would do it like this:

$('ul#catA').children('li').each(function (i)
{
    if (i > 19)
    {
        $(this).hide();   
    }
});

var more = $('<li class="cat-item"><a href="">Show More</a></li>');
$('ul#catA').append(more);

more.find('a').on('click', function (){
   $(this).parent().siblings('li').show();

   return false; 
});

Here is the jsfiddle showing my method in action.


I created an updated jsfiddle based on @nachito's recommendation on using the :gt() selector. I also added some comments explaining what's happening.

// creating the new 'Show More' link
var more = $('<li class="cat-item"><a href="">Show More</a></li>');

// hiding all lis after the first two
$('ul#catA').children('li:gt(1)').hide();

// adding the 'Show More' link
$('ul#catA').append(more);

// binding a click event to 'Show More'
more.find('a').on('click', function (){        
   // hiding 'Show More' and showing the rest of the lis in this ul 
   $(this).parent().hide().siblings('li').show();

   // preventing default action and event bubbling
   return false; 
});
浅语花开 2024-12-28 18:32:24

这是最简单的解决方案,但可能有效:

  • 让“查看全部”链接在列表后创建,并为其指定一个 ID,如“#catA-viewall”;
  • 隐藏所有项目;
  • 只显示前20条;
  • 点击“查看全部”后,我们会显示所有元素。

    var $items = $('#catA').children();
    if ( $items.length > 20 ) {
        $items.hide().slice(0, 20).show();
        $('#catA-viewall').click(function () {
            $items.fadeIn(); // 或 .show()
            $(this).remove();
        });
    } 别的 {
        $('#catA-viewall').remove();
    }
    

This is the simplest solution, but may work:

  • Let the 'view all' link created after the list, and give it an ID like '#catA-viewall';
  • Hide all items;
  • Show only the first 20;
  • On click on 'view all', we show all the elements.

    var $items = $('#catA').children();
    if ( $items.length > 20 ) {
        $items.hide().slice(0, 20).show();
        $('#catA-viewall').click(function () {
            $items.fadeIn(); // or .show()
            $(this).remove();
        });
    } else {
        $('#catA-viewall').remove();
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文