2013-01-13 67 views
-1

我正在尝试在变量中保存一些HTML(来自XML文件),但不知何故,我收到了一些奇怪的错误。除此之外,即使我不使用它,浏览器似乎也会尝试加载这段HTML代码中使用的图像。将HTML保存在变量中的JavaScript

下面的代码:

// Load the data from the XML node. 
var description = $(this).find('description').text(); 

var descriptionWithTags = description.replace('<p>',''); 
descriptionWithTags = descriptionWithTags.replace('</p>','########'); 

// Remove HTML Tags and add the Line-Breaks 
var convertedString = $(descriptionWithTags).text(); 
convertedString = convertedString.replace('########','<br /><br />'); 
console.log("E"); 

// Save it in an array. 
contentRSS[articleCount] = convertedString; 

编辑:如果我删除以下行,它再次工作,但我现在为什么不。

descriptionWithTags = descriptionWithTags.replace('<p>',''); 
+3

...,什么是这些难以捉摸的'怪errors'? –

+0

它说jquery.js中有错误,但是显示的是我加载的html内容的一部分。它看起来像是在混合一些东西。 –

回答

2

开始时你有没有文字的HTML标签:

var description = $(this).find('description').text(); // this removes HTML tags 

然后尝试解析它作为HTML:

var convertedString = $(descriptionWithTags).text(); 

这可不行。你或许应该在第一行用html:

var description = $(this).find('description').html(); 
+0

你好,谢谢你的回答。我不确定,但我想我需要第二个.text(),因为它会去掉html标签。第一个.text()只是删除加载的竞争对象的。这是一个笔记样本。 <![CDATA [一些HTML代码在这里。 ]]>

+0

问题主要在于第一个问题:您不能要求原始文本(从HTML标记中清除),然后要求将其解释为HTML。 –