【发布时间】:2014-12-22 03:31:48
【问题描述】:
我正在使用Google Feed API 从tumblr feed 中提取博客条目。
我已经能够提取内容,但是输出带有 html 标签:
<p>I remember one day asking one of my mentors James if he ever got nervous around people. James replied, “Only when I need something from them.”</p>
代码很简单,如下:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("feeds", "1");
function initialize() {
var feed = new google.feeds.Feed("http://adriennetran.tumblr.com/rss");
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
window.content = document.createTextNode(entry.content);
container.appendChild(content);
}
}
});
}
google.setOnLoadCallback(initialize);
</script>
我尝试编写一个函数来删除以< 开头的所有内容:
content_array = content.split(" ");
for (i=0; i < content_array.length; i++){
if ((content_array[i].split(""))[0] == "<"){
content_array.splice(i, 1);
}
}
content2 = content_array.toString();
但我收到了 Uncaught TypeError: undefined is not a function 错误,因为 content 是 object 而不是 string,因此我无法调用 content.split(" ")。
我试过转换成字符串,但这是控制台的输出
typeof(content)
> "object"
c2 = content.toString()
> "[object Text]"
有人对如何操作从 RSS 检索到的元素有任何想法吗?
【问题讨论】:
-
为了提供进一步的服务,您能提供一个 JSfiddle 吗?
标签: javascript rss