Node.js htmlparser2如何用另一个具有与标记相同属性的自定义标记替换标记
我需要将一个标记更改为具有相同属性的另一个标记。例如,更改此项:Node.js htmlparser2如何用另一个具有与标记相同属性的自定义标记替换标记,node.js,html-parsing,html-parser,Node.js,Html Parsing,Html Parser,我需要将一个标记更改为具有相同属性的另一个标记。例如,更改此项: <TagOne width="500" height="200">asdfasdf</TagOne> 但它给了我一个错误,这不是正确的方法,但我在文档中找不到任何类似的方法。有人能帮我吗?谢谢 <AnotherTag width="500" height="200">sometext</AnotherTag> const htmlparser2 = require("htmlpa
<TagOne width="500" height="200">asdfasdf</TagOne>
但它给了我一个错误,这不是正确的方法,但我在文档中找不到任何类似的方法。有人能帮我吗?谢谢
<AnotherTag width="500" height="200">sometext</AnotherTag>
const htmlparser2 = require("htmlparser2");
const DomUtils = require("htmlparser2").DomUtils;
const htmlContent = `<html>
<head></head> <body> <div id="content">
<TagOne width="500" height="200" src="image1.jpg">asdfasdf</TagOne>
<p>asdfasdf</p> </div></body></html>`; const parser = new htmlparser2.Parser(
{
onopentag(name, attribs) {
if (name === "TagOne") {
if(attribs.width ){
var width = attribs.width;
}
if(attribs.height ){
var height = attribs.height;
}
var new_tag = `<AnotherTag width:`+width+`; height:`+height+`;">sometext</div>`;
}
},
ontext(text) {
console.log("-->", text);
},
onclosetag(tagname,new_tag) {
if (tagname === "amp-iframe") {
console.log("That's it?!");
}
},
},
{ decodeEntities: true } );
var content = parser.write(htmlContent);
parser.end();
//htmlparser2.DomUtils.removeElement(tagname);
//parser.write(new_tag);