我在将变量放入数组时遇到问题。这是我的代码:
var info = new Array();
google.load("feeds", "1");
function initialize() {
var feed = new google.feeds.Feed("http://www.ntvmsnbc.com/id/24927681/device/rss/rss.xml");
feed.setNumEntries(6);
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
var html = '';
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var a = " " ;
a += entry.title;
info[i] = a
html += '<p>' + entry.publishedDate + ' ' + entry.title;
}
container.innerHTML = html;
}
alert(info[0]);
});
//alert(info[0]);
}
//alert(info[0]);
google.setOnLoadCallback(initialize);
您会看到一个名为 info 的数组。我正在尝试将 entry.title 添加到其中。你可以看到有些地方被注释掉了。我的信息[0] 是空的。我只能在函数(结果)中显示我的结果,除了它不显示任何东西,就像我从未在我的数组中放入任何东西一样。我不明白为什么。
更新:
var info = new Array();
google.load("feeds", "1");
function initialize(cb) {
var feed = new google.feeds.Feed("http://www.ntvmsnbc.com/id/24927681/device/rss/rss.xml");
feed.setNumEntries(6);
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
var html = '';
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
html += '<p>' + entry.publishedDate + ' ' + entry.title;
cb(entry.title);
}
container.innerHTML = html;
}
});
}
google.setOnLoadCallback(function(){
initizalize(processInfo);}
);
function processInfo(information){
info[info.length] = information;
alert(info[info.length]);
}
最新版本
var info = new Array();
google.load("feeds", "1");
function initialize(cb) {
var feed = new google.feeds.Feed("http://www.ntvmsnbc.com/id/24927681/device/rss/rss.xml");
feed.setNumEntries(6);
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
var html = '';
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var a= " ";
a += entry.title;
info[i] = a;
html += '<p>' + entry.publishedDate + ' ' + entry.title;
}
container.innerHTML = html;
}
cb(info);
});
}
google.setOnLoadCallback(function(){
initizalize(processInfo);}
);
function processInfo(info){
alert(info[0]);
}