首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在createTextNode中使用innerHTML?

如何在createTextNode中使用innerHTML?
EN

Stack Overflow用户
提问于 2011-11-07 08:40:42
回答 1查看 7.6K关注 0票数 3

我正在尝试创建一个chrome扩展,我通过使用XMLHttpRequest解析RSS提要,然后将解析后的令牌(如title & description )存储到一个数组中来实现此目的。

在解析提要并将标记存储到一个数组中之后,我遍历该数组,并使用DOM将它们显示在chrome扩展弹出窗口上。它可以工作,但问题是每个description都有超文本标记语言标签,并显示如下所示:

<p><img src="http://www.documentarywire.com/cover/college-inc.jpg" width="90" height="100" style="float:left; padding: 0 10px 20px 0"/></p>Even in lean times, the $400 billion business of higher education is booming. Nowhere is this more true than in one of the fastest-growing &#8212; and most controversial &#8212; sectors of the industry: for-profit colleges and universities that cater to non-traditional students, often confer degrees over the Internet, and, along the way, successfully capture billions [...]

超文本标记语言标签来自feed,我没有任何问题,我想做的就是让它们真正被解释为超文本标记语言而不是文本。我相信这是因为我使用了createTextNode?我应该使用innerHTML?我不确定如何在下面的例子中使用innerHTML?

代码语言:javascript
复制
   var descriptionNode = document.createElement("div");
   descriptionNode.setAttribute("class", "description");
   descriptionNode.appendChild(document.createTextNode(item["description"]));
   detail.appendChild(descriptionNode);

如果有帮助的话,我可以上传更多的代码?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-07 08:46:28

尝试:

代码语言:javascript
复制
var descriptionNode = document.createElement("div");
descriptionNode.className = "description";
descriptionNode.innerHTML = item["description"];
detail.appendChild(descriptionNode);

createTextNode创建一个文本节点,传递给它的字符串被视为纯文本,并被分配给该节点的data属性。分配给innerHTML属性的字符串被视为标记,由浏览器的HTML解析器(如果是XML文档,则是最新的浏览器中的解析器)解析并转换为DOM元素。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8031604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档