Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js htmlparser2如何用另一个具有与标记相同属性的自定义标记替换标记_Node.js_Html Parsing_Html Parser - Fatal编程技术网

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);