简体   繁体   English

如何隐藏列表中的元素并添加“显示更多”功能?

[英]How can I hide elements in my list and add a 'show more' feature?

I'm using javascript to build a list of results.我正在使用 javascript 来构建结果列表。 I have a for-loop that iterates over some data and creates a mydata div, and adds that to the results div.我有一个 for 循环,它遍历一些数据并创建一个 mydata div,并将其添加到结果 div。 Let's pretend it looks something like this:让我们假设它看起来像这样:

<div id="results">
    <div class="mydata">data 1</div>
    <div class="mydata">data 2</div>
    ...
    <div class="mydata">data 20</div>
</div>

What I want to do is only display 5 results at a time , and should the user wish to see more, they can click a show next 5 or show more button (or something similar).我想要做的是一次只显示 5 个结果,如果用户希望看到更多,他们可以单击显示下一个 5显示更多按钮(或类似的东西)。 Any ideas?有任何想法吗?

Just to clarify, every time the user clicks "show more" I want to 'unhide' the next 5 elements, not ALL the remaining elements.澄清一下,每次用户单击“显示更多”时,我都想“取消隐藏”接下来的 5 个元素,而不是所有剩余的元素。 So each click reveals more elements until all are displayed.因此,每次单击都会显示更多元素,直到所有元素都显示出来。

You can use the gt() and lt() selectors along with :visible pretty well here.您可以在这里很好地使用gt()lt()选择器以及:visible

The following will show the next 5 results on clicking and removes the link once all items are visible.以下将显示单击后的下 5 个结果,并在所有项目可见后删除链接。

$('.mydata:gt(4)').hide().last().after(
    $('<a />').attr('href','#').text('Show more').click(function(){
        var a = this;
        $('.mydata:not(:visible):lt(5)').fadeIn(function(){
         if ($('.mydata:not(:visible)').length == 0) $(a).remove();   
        }); return false;
    })
);

working example: http://jsfiddle.net/niklasvh/nTv7D/工作示例: http://jsfiddle.net/niklasvh/nTv7D/

Regardless of what other people are suggesting here, I would not hide the elements using CSS, but do it in JS instead, because if a user has JS disabled and you hide the elements using CSS, he won't get them visible.不管其他人在这里建议什么,我都不会使用 CSS 隐藏元素,而是在 JS 中进行,因为如果用户禁用了 JS,而您使用 CSS 隐藏元素,他将不会让它们可见。 However, if he has JS disabled, they will never get hidden, nor will that button appear etc, so it has a full noscript fallback in place + search engines don't like hidden content (but they won't know its hidden if you do it on DOM load).但是,如果他禁用了 JS,它们将永远不会被隐藏,该按钮也不会出现等,所以它有一个完整的 noscript 后备 + 搜索引擎不喜欢隐藏的内容(但如果你不知道它是隐藏的在 DOM 加载时执行此操作)。

My solution is here: jsFiddle .我的解决方案在这里: jsFiddle

You can put this link somewhere:您可以将此链接放在某处:

<a href="#" title="" id="results-show-more">show more</a>

and use the following code:并使用以下代码:

var limit = 5;
var per_page = 5;
jQuery('#results > div.mydata:gt('+(limit-1)+')').hide();
if (jQuery('#results > div.mydata').length <= limit) {
    jQuery('#results-show-more').hide();
};

jQuery('#results-show-more').bind('click', function(event){
    event.preventDefault();
    limit += per_page;
    jQuery('#results > div.mydata:lt('+(limit)+')').show();
    if (jQuery('#results > div.mydata').length <= limit) {
        jQuery(this).hide();
    }
});

where limit is your current number of results displayed and per_page is number of results shown with each click on "show more".其中limit是您当前显示的结果数, per_page是每次单击“显示更多”时显示的结果数。 The link disappears if all the results are displayed.如果显示所有结果,链接就会消失。 See how it works on jsFiddle .看看它在 jsFiddle 上是如何工作的。

You can create a CSS class like:您可以创建一个 CSS class 像:

.hiddenData { display: none }

and attach it to any quantity of divs that exceeds 5.并将其附加到任何数量超过 5 的 div 上。

After that make handlers for adding/deleting this class from the needed quantity of divs.之后,制作用于从所需数量的 div 中添加/删除此 class 的处理程序。

jQuery for class removing: jQuery 用于 class 移除:

$(".hiddenData").removeClass("hiddenData")

Create a class with something like:使用以下内容创建 class:

.hidden_class{
  display: none;
}

Add this class to all the mydata div's that you dont want seen.将此 class 添加到您不想看到的所有 mydata div 中。 when the user click the button, remove it from the next 5 div's.当用户单击按钮时,将其从接下来的 5 个 div 中删除。

repeat everytime the user clicks the "read more" button每次用户单击“阅读更多”按钮时重复

This should work...Let me know how it goes这应该工作......让我知道它是怎么回事

<script type="text/javascript">
  function ShowHide(id) { $("#" + id).toggle(); }
</script>
<div id="results">
    <div class="mydata">data 1</div>
    <div class="mydata">data 2</div>
    <div class="mydata">data 3</div>
    <div class="mydata">data 4</div>
    <div class="mydata">data 5</div>

    <div style="clear:both" onclick="ShowHide('grp6')">More</div>
    <div id="grp6" style="display:none">
      <div class="mydata">data 6</div>
      <div class="mydata">data 7</div>
      <div class="mydata">data 8</div>
      <div class="mydata">data 9</div>
      <div class="mydata">data 10</div>
    </div>
    <div style="clear:both" onclick="ShowHide('grp11')">More</div>
    <div id="grp11" style="display:none">
      <div class="mydata">data 11</div>
      <div class="mydata">data 12</div>
      <div class="mydata">data 13</div>
      <div class="mydata">data 14</div>
      <div class="mydata">data 15</div>
    </div>
</div>

In your forloop, you also have to add these divs hidden container在您的 forloop 中,您还必须添加这些 div 隐藏容器

<div style="clear:both" onclick="ShowHide('grp6')">More</div>
    <div id="grp6" style="display:none">

You get the idea.你明白了。

Here you have:在这里你有:

   <style>
      /*This hides all items initially*/
      .mydata{
         display: none;
      }
   </style>

Now the script现在脚本

   <script>
      var currentPage = 1; //Global var that stores the current page
      var itemsPerPage = 5;
      //This function shows a specific 'page'
      function showPage(page){
          $("#results .mydata").each(function(i, elem){
              if(i >= (page-1)*itemsPerPage && i < page*itemsPerPage) //If item is in page, show it
                $(this).show();
              else
                $(this).hide();
          });
          $("#currentPage").text(currentPage);
      }
      $(document).ready(function(){
          showPage(currentPage);
          $("#next").click(function(){
             showPage(++currentPage);
          });
          $("#prev").click(function(){
             showPage(--currentPage);
          });
      });
   </script>

And a sample html:还有一个样品 html:

   <div id="results">
      <div class="mydata">data 1</div>
      <div class="mydata">data 2</div>
      <div class="mydata">data 3</div>
      <div class="mydata">data 4</div>
      <div class="mydata">data 5</div>
      <div class="mydata">data 6</div>
      <div class="mydata">data 7</div>
      <div class="mydata">data 8</div>
      <div class="mydata">data 9</div>
      <div class="mydata">data 10</div>
      <div class="mydata">data 11</div>
      <div class="mydata">data 12</div>
   </div>   
   <a href="javascript:void(0)" id="prev">Previous</a>
   <span id="currentPage"></span>
   <a href="javascript:void(0)" id="next">Next</a>

The only thing remaining is to validate fot not going to a page lower than 1 and higher than the total.剩下的唯一一件事是验证 fot 不会转到低于 1 且高于总数的页面。 But that will be easy.但这很容易。

EDIT : Here you have it running: http://jsfiddle.net/U8Q4Z/编辑:在这里你运行它: http://jsfiddle.net/U8Q4Z/

Hope this helps.希望这可以帮助。 Cheers.干杯。

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

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