HTML如何使用DOM createComment()方法(代码示例)

的createComment()方法用于创建带有某些指定文本的注释节点。此属性用于将文本值设置为字符串类型的参数。
语法如下:

document.createComment( text )

参数:该方法接受可选的单个参数文本。此参数用于保存注释字符串。
例子:
< !DOCTYPE html> < html > < head > < title > DOM createComment() Method< / title > < style > h1, h2 { color:green; font-weight:bold; } body { text-align:center; } < / style > < / head > < body > < h1 > lsbin< / h1 > < h2 > DOM createComment() Method< / h2 > < button onclick = "geeks()" > Submit< / button > < p id = "sudo" > < / p > < script > function geeks() { var c = document.createComment("GFG comments"); document.body.appendChild(c); var x = document.getElementById("sudo"); x.innerHTML = "Comments are Invisible!"; } < / script > < / body > < / html >

输出如下:
HTML如何使用DOM createComment()方法(代码示例)

文章图片
【HTML如何使用DOM createComment()方法(代码示例)】支持的浏览器:支持的浏览器DOM createComment()方法属性如下:
  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • 歌剧
  • 苹果浏览器

    推荐阅读