html 如何让cheerio不自行关闭标签?

r6hnlfcb  于 2022-12-21  发布在  其他
关注(0)|答案(1)|浏览(107)

cheerio下此代码的输出:

<div id="some"></div>

返回为自结束标记:

<div id="some"/>

有可能改变这种行为(自动关闭)吗?我不想使用它。
我尝试了这些选项,但似乎不影响输出:

{ decodeEntities: false, xmlMode: true, recognizeSelfClosing: false },

是否有其他解决方案可用?

import "./styles.css";
import { CheerioAPI, load } from "cheerio";

console.clear();

const doc = load(
  `
  <div id="some"></div>
`,
  { decodeEntities: false, xmlMode: true, recognizeSelfClosing: false },
  false
);

console.log({
  after: doc.html({
    decodeEntities: false,
    xmlMode: true,
    recognizeSelfClosing: false
  })
});

codesandbox.io

6mzjoqzu

6mzjoqzu1#

您已设置xmlMode:true,需要设置xmlMode:false并删除recognizeSelfClosing: false

import "./styles.css";
import { CheerioAPI, load } from "cheerio";

console.clear();

const doc = load(
  `
  <div id="some"></div>
`,
  { decodeEntities: false, xmlMode: true, recognizeSelfClosing: false },
  false
);

console.log({
  after: doc.html({
    decodeEntities: false,
    xmlMode: false
  })
});

这将在控制台中给予如下输出

{after: "
  <div id="some"></div>
"}

相关问题