使用 nodeJS 和 cheerio 写入 HTML 文件时获取特殊字符而不是单引号
getting special character instead of single quote when writing to HTML file using nodeJS and cheerio
我只是在玩节点,所以我想在我的 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} );
我只是在玩节点,所以我想在我的 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} );