node.js - 使用nodeJS和cheerio写入HTML文件时获取特殊字符而不是单引号
问题描述
我只是在玩节点,所以我想在我的 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
decodeEntities: false
默认情况下正在解码 HTML 实体,需要时您可以通过传递选项关闭此功能
这是一个例子:
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ), {decodeEntities: false} );
推荐阅读
- git - 如何解决 src refspec dev 不匹配任何
- python - Selenium Python XPath查找具有子/孙的元素
- macos - 有什么方法可以在 MacOS Catalina(临时)上启用 TLS 1.0 来检查旧软件?
- java - 无法在 GoCD 上实例化测试容器
- python - Sqlite 中的压缩 apache 日志
- flutter - Cocoapods 没有正确更新 Flutter pod
- vulkan - VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM 的内存布局
- delphi - Delphi FMX tComboBox.ListBox 上的滚动条未在 Mac 上显示
- ios - UIView 内容未在 UIKit Swift 中更新
- css - 使用带有 scss 的字体真棒 ttf