如何将innerHTML与createTextNode一起使用? [英] How do I use innerHTML with createTextNode?

查看:114
本文介绍了如何将innerHTML与createTextNode一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个chrome扩展程序并通过解析带有XMLHttpRequest的RSS提要然后存储已解析的标记来执行此操作,例如 title & description ,进入一个数组。

I'm trying to create a chrome extension and im doing this by parsing an RSS feed with XMLHttpRequest and then storing the tokens parsed, such as title & description, into an array.

在解析了feed并将令牌存储到一个数组中后,我循环遍历数组并使用DOM将它们显示在chrome扩展弹出窗口中。它的工作原理但问题是每个描述都有HTML标签并显示如下内容:

After parsing the feed and storing the tokens into an array, I then loop through the array and display them onto the chrome extension popup using DOM. It works but the problem is that each description has HTML tags and shows up something like this:

< p>< img src =http://www.documentarywire.com/cover/college-inc.jpgwidth =90height =100style =float:left; padding: 0 10px 20px 0/>< / p>即使在贫困时期,4000亿美元的高等教育业务也在蓬勃发展。没有什么比增长最快的&#8212;最有争议的&#8212;行业部门:迎合非传统学生的营利性学院和大学,通常通过互联网授予学位,并且一路上成功捕获数十亿[...]

HTML标签来自 Feed ,我有他们没有问题,我想做的就是将它们实际解释为HTML而不是文本。我相信这是因为我使用 createTextNode ?我应该使用 innerHTML?我不确定如何在以下示例中使用innerHTML?

The HTML tags come from the feed and I have no problem with them all I want to do is get them to be actually interpreted as HTML rather than as text. I believe this is because im using createTextNode? I should be using innerHTML? I'm not sure how to use innerHTML in the following example?

   var descriptionNode = document.createElement("div");
   descriptionNode.setAttribute("class", "description");
   descriptionNode.appendChild(document.createTextNode(item["description"]));
   detail.appendChild(descriptionNode);

如果有帮助,我可以发布更多代码?

I can post more of my code up if that will help?

推荐答案

尝试:

var descriptionNode = document.createElement("div");
descriptionNode.className = "description";
descriptionNode.innerHTML = item["description"];
detail.appendChild(descriptionNode);

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

createTextNode creates a text node, the string passed to it is treated as plain text and assigned to the node's data property. The string assigned to the innerHTML property is treated as markup and is parsed by the browser's HTML parser (or XML parser in very recent browsers in the case of an XML document) and turned into DOM elements.

这篇关于如何将innerHTML与createTextNode一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆