剧本:
function get_rss_data(feed_url, callback) {
$.ajax({
url: feed_url,
type: 'POST',
success: function(data) {
callback(data);
}
});
}
function get_news(feed_url="http://....") {
var news = [];
get_rss_data(feed_url, function (data) {
var entries = data.getElementsByTagName("entry");
for (var x = 0; x < entries.length; x++) {
var title = entries[x].getElementsByTagName("title")[0].textContent.substring(7);
var content = entries[x].getElementsByTagName("content")[0].textContent.split('<p>')[0].substring(1);
var author = entries[x].getElementsByTagName("author")[0].textContent;
var updated = String(new Date(entries[x].getElementsByTagName("updated")[0].textContent)).substring(0,25);
var news_item = {title:title, content:content, author:author, updated:updated};
news.push(news_item);
}
});
return news;
}
电话:
<script>
var news = get_news();
console.log(news);
</script>
Console output
问题是我正在尝试访问数组中的数据,但在使用
news[0].content;
和
news.length;
返回0
数组中的内容我无法访问它。
我将脚本放在一个单独的.js文件中,并从index.html运行console.log