最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript: limit number of items - Stack Overflow

programmeradmin3浏览0评论

sorry but I'm new in javascrit. I'm writing a simple rss reader, but I want to limit the number of feeds to 5 items, because the my target site can have 100 or more. Here the code:

$("#mainPage").live("pageinit", function () {
    $("h1", this).text(title);
    $.get(RSS, {}, function (res, code) {
    var xml = $(res);
    var items = xml.find("item");
    var items = $items.length && i < 5; i++); // here the problem!!
    var entry = "";
            $.each(items, function (i, v) {
            entry = {
                 title:$(v).find("title").text(),
                 link:$(v).find("link").text(),
                 description:$.trim($(v).find("encoded").text()),
                 category:$.trim($(v).find("category").text()),
                 date:$(v).find("pubDate").text().substr(0,16),
                 autor:$(v).find("creator").text()
            };
             entries.push(entry);
         });
     var s = '';
     $.each(entries, function(i, v) {
        s += '<li><a href="#contentPage" class="contentLink" data-entryid="'+i+'">' + v.title + '<br><i>' + v.autor + ' - ' + v.date + '</i></a></li>';
        });
        $("#linksList").append(s);
         $("#linksList").listview("refresh");
    });
});

The problem is that when I try to limit the number of items adding var items = $items.length && i < 5; i++); the javascript stop to works. :-(

How to do it?

sorry but I'm new in javascrit. I'm writing a simple rss reader, but I want to limit the number of feeds to 5 items, because the my target site can have 100 or more. Here the code:

$("#mainPage").live("pageinit", function () {
    $("h1", this).text(title);
    $.get(RSS, {}, function (res, code) {
    var xml = $(res);
    var items = xml.find("item");
    var items = $items.length && i < 5; i++); // here the problem!!
    var entry = "";
            $.each(items, function (i, v) {
            entry = {
                 title:$(v).find("title").text(),
                 link:$(v).find("link").text(),
                 description:$.trim($(v).find("encoded").text()),
                 category:$.trim($(v).find("category").text()),
                 date:$(v).find("pubDate").text().substr(0,16),
                 autor:$(v).find("creator").text()
            };
             entries.push(entry);
         });
     var s = '';
     $.each(entries, function(i, v) {
        s += '<li><a href="#contentPage" class="contentLink" data-entryid="'+i+'">' + v.title + '<br><i>' + v.autor + ' - ' + v.date + '</i></a></li>';
        });
        $("#linksList").append(s);
         $("#linksList").listview("refresh");
    });
});

The problem is that when I try to limit the number of items adding var items = $items.length && i < 5; i++); the javascript stop to works. :-(

How to do it?

Share Improve this question asked Dec 10, 2015 at 9:56 maxlinux2000maxlinux2000 391 silver badge3 bronze badges 3
  • any error in the console ? something like SyntaxError – Hacketo Commented Dec 10, 2015 at 9:59
  • invalid javascript... – Michal Paszkiewicz Commented Dec 10, 2015 at 10:01
  • 1 I'm new in stackoverflow too. :) so sorry if i'm making errors in the local netiquette. TNX to Thomas! Your solutions is working. TNX again – maxlinux2000 Commented Dec 10, 2015 at 10:17
Add a ment  | 

2 Answers 2

Reset to default 7

var items = $items.length && i < 5; i++); is invalid I think you want to

var items = items.slice(0, 4); is what you want.

https://api.jquery./slice/ (as pointed out to me, it's a jQuery object)

The jQuery object itself behaves much like an array; it has a length property and the elements in the object can be accessed by their numeric indices [0] to [length-1]. Note that a jQuery object is not actually a Javascript Array object, so it does not have all the methods of a true Array object such as join().

Because Javascript is 0-based (starts to count from 0) you want element 0 to 4, in order to get the first 5 elements.

You can use lt(n) function

var items = xml.find("item:lt(5)");
发布评论

评论列表(0)

  1. 暂无评论