JavaScript将HTML保存在一个变量中

JavaScript saving HTML in a variable

本文关键字:一个 变量 HTML 保存 存在 JavaScript      更新时间:2023-09-26

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

代码如下:

// 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>','');

你从一个没有HTML标签的文本开始:

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

然后尝试解析为HTML:

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

这不能工作。你应该在第一行使用html:

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