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
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 . If you want to access comment nodes, make sure they are descendants of the 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 (m.sbmmt.com)!