在使用nodeJS和cheerio编写HTML文件时,获得特殊字符而不是单引号



我只是在玩node,所以我想在我的html文件中替换以下html

<div class="replace" onclick="opennewtab('one')">

想把它换成

<div class="replace" onclick="replacedFunc('12345&')">

所以我有以下节点代码:

let fs = require('fs'),
cheerio = require('cheerio');
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ) );
$('.replace').attr('onclick' , "replacedFunc('12345')");
console.log($.html());
inner_content =  $.html();
fs.writeFileSync( `${__dirname}/res/newwritetojs.html` , inner_content, 'utf8');

但我得到的是

<div class="replace" onclick="replacedFunc(&apos;12345&apos;)">

如何获得'而不是&apos;??

Cheerio默认解码HTML实体,需要时可以通过传递decodeEntities: false选项来关闭此功能

这里有一个例子:

$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ), {decodeEntities: false} );

最新更新