简体   繁体   English

jQuery加载列表问题

[英]jQuery Load List Issue

I am using this function to "load more" list items as required. 我正在使用此功能根据需要“加载更多”列表项。

The load more function I am using: http://jsfiddle.net/cse_tushar/6FzSb/2/ 我正在使用的加载更多功能: http : //jsfiddle.net/cse_tushar/6FzSb/2/

I am populating my list with PHP. 我用PHP填充列表。 My problem is that all the list items are displayed when the page loads. 我的问题是页面加载时会显示所有列表项。 When I click on load more the functionality starts working and I can load more or load less. 当我单击“更多加载”时,该功能开始工作,并且可以加载更多或更少。 Only 3 list items should be displayed on page load (x=3). 页面加载(x = 3)时仅应显示3个列表项。

Any ideas how I can resolve this? 有什么想法可以解决这个问题吗?

Thanks in advance! 提前致谢!

List code: 清单代码:

<?php include('comments.php'); 

                /*
                /   Output the comments one by one:
                */
                foreach($comments as $c){

                    echo $c->markup();
                }

                ?>

comments.php (simplified): comments.php(简体):

    class Comment
    {
public function markup()
    {

            return '<li>$text</li>' };

};

The JS of the load more: JS的负载更多:

$(document).ready(function () {
    size_li = $("#myList li").size();
    x=3;
    $('#myList li:lt('+x+')').show();
    $('#loadMore').click(function () {
        x= (x+5 <= size_li) ? x+5 : size_li;
        $('#myList li:lt('+x+')').show();
         $('#showLess').show();
        if(x == size_li){
            $('#loadMore').hide();
        }
    });
    $('#showLess').click(function () {
        x=(x-5<0) ? 3 : x-5;
        $('#myList li').not(':lt('+x+')').hide();
        $('#loadMore').show();
         $('#showLess').show();
        if(x == 3){
            $('#showLess').hide();
        }
    });
});

To only output 3 times, then you need to install a counter, (a simple variable) to count how many times you've printed the markup. 要只输出3次,则需要安装一个计数器(一个简单的变量)以计算打印标记的次数。 You can do something like this to accomplish that goal: 您可以执行以下操作来实现该目标:

$i = 0;
foreach($comments as $c){
  $i++;
  if($i < 4) {
    echo $c->markup();
  }
 }

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

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