我只是在玩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('12345')">
如何获得'
而不是'
??
Cheerio
默认解码HTML实体,需要时可以通过传递decodeEntities: false
选项来关闭此功能
这里有一个例子:
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ), {decodeEntities: false} );