Home > Web Front-end > JS Tutorial > JavaScript Document Object Model-Comment type

JavaScript Document Object Model-Comment type

黄舟
Release: 2017-01-20 14:41:51
Original
1905 people have browsed it

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>
Copy after login

In the above code, the comment node is a child node of the

l element. It can be accessed through the following code:

var div = document.getElementById("myDiv");
var comment = div.firstChild;
console.info(comment.data);       // "一个注释内容"
Copy after login

To create a comment, use the document.createComment() method and pass the comment content as a parameter. For example:

var comment = document.createComment("注释内容");
Copy after login

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 (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template