createComment() method


HTML DOM createComment() Method

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p id="demo">单击按钮改HTML文档添加一个注释</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var c=document.createComment("My personal comments");
	document.body.appendChild(c);
	var x=document.getElementById("demo");
	x.innerHTML="这个文件现在包含一个注释,但如你所知,注释是看不见的。";
};
</script>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example


Definition and usage

The createComment() method can create a comment node.


Browser support

QQ截图20161108165429.png

All major browsers support the createComment() method


Syntax

document.createComment(text)

Parameters

Parameters TypeDescription
textStringOptional. Comment text to add.

Return value

TypeDescription
Comment objectCreated comment node

Technical details

DOM versionCore Level 1 Document Object