Home >Web Front-end >JS Tutorial >JavaScript Document Object Model-Comment type
Comment content is represented by the Comment type in the DOM document. The Comment node has the following characteristics:
The value of nodeType is 8.
The value of nodeName is "#comment".
The value of nodeValue is the content of the comment.
parentNode may be a Document or Element.
It has no child nodes.
Comment type inherits from the same base class as Text type, therefore, it has all string operation methods except splitText(). Similar to the Text type, the content of the comment can also be obtained through the nodeValue or data attribute.
The comment node can be accessed through its parent node, take the following code as an example:
<div id="myDiv"><!-- 一个注释内容 --></div>
In the above code, the comment node is a child node of the dc6dce4a544fdca2df29d5ac0ea9906bl element. It can be accessed through the following code:
var div = document.getElementById("myDiv"); var comment = div.firstChild; console.info(comment.data); // "一个注释内容"
To create a comment, use the document.createComment() method and pass the comment content as a parameter. For example:
var comment = document.createComment("注释内容");
In actual operations, we usually do not create and access comment nodes, because comment nodes basically have no impact on the entire DOM algorithm.
In addition, the browser will not recognize the comment content located after 73a6ac4ed44ffec12cee46588e518a5e. If you want to access comment nodes, make sure they are descendants of the 100db36a723c770d327fc0aef2ce13b1 element.
The above is the content of JavaScript Document Object Model-Comment type. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!