文档:createComment() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

createComment() 创建一个新的注释节点,并返回该节点。

语法

js
createComment(data)

参数

data

包含要添加到 Comment 中的数据的字符串。

返回值

一个新的 Comment 对象。

示例

js
const doc = new DOMParser().parseFromString("<xml></xml>", "application/xml");
const comment = doc.createComment(
  "This is a not-so-secret comment in your document",
);

doc.querySelector("xml").appendChild(comment);

console.log(new XMLSerializer().serializeToString(doc));
// Displays: <xml><!--This is a not-so-secret comment in your document--></xml>

规范

规范
DOM
# ref-for-dom-document-createcomment①

浏览器兼容性