admin管理员组文章数量:1317898
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
2 Answers
Reset to default 7var 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)");
本文标签: jqueryjavascript limit number of itemsStack Overflow
版权声明:本文标题:jquery - javascript: limit number of items - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742034212a2417029.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论