简体   繁体   English

使用未转义的HTML将元素的内容作为字符串

[英]Getting content of an element as string with unescaped HTML

I'm using xmldom parser from npm library, it's based on the DOMNode object model. 我正在使用来自npm库的xmldom解析器,它基于DOMNode对象模型。 Having the following code: 拥有以下代码:

var xml = "<p>Test</p><toFetch id="1">test\n\n<p>aaa</p>test\n</toFetch>";
var parser = new dom.DOMParser().parseFromString(xml, "text/xml");

I want to get the content of the toFetch tag, as a string with all unescaped HTML tags inside, and without the toFetch tag itself. 我想获取toFetch标记的内容,作为一个包含所有未转义HTML标记的字符串,并且没有toFetch标记本身。

What I have is: 我有的是:

var elements = parser.getElementsByTagName("toFetch");
elements.forEach(element => {
    console.log(element.toString());
});

It works, but it gives me the string with toFetch surrounding tag. 它工作,但它给我带有toFetch周围标签的字符串。 How can I get only the content? 我怎样才能获得内容?

Your string has a typo in it. 你的字符串中有一个拼写错误。 The 1 should be in single quotes, not doubles. 1应该是单引号,而不是双引号。

Then, change: console.log(element.toString()); 然后,更改: console.log(element.toString());

To: console.log(element.textContent); To: console.log(element.textContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM