How to create nodes in Javascript
Most people do not understand the knowledge points of this article "how to create nodes in Javascript", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to create nodes in Javascript" article.
There are several ways for Javascript to create nodes
The following is a list of common ways to create nodes:
Method description
CreateElement () creates an element node
CreateTextNode () creates a text node
CreateComment () creates a text node
CreateDocumentFragment () creates a document fragment node
The above four methods are all methods of the document object.
CreateElement ()
CreateElement () is used to create an element node, the node of nodeType=1.
Syntax:
Document.createElement (tagName)
Where tagName is the name of the HTML tag and will return a node object.
For example, the statements to create div > tags and p > tags are as follows:
Var ele_div=document.createElement ("div")
Var ele_p=document.createElement ("p")
CreateTextNode ()
CreateTextNode () is used to create a text node, the node of nodeType=3.
There are several ways for Javascript to create nodes
Syntax:
Document.createTextNode (text)
Where text is the content of the text node and will return a node object.
For example, create a text node with the content "this is a text node":
?
one
Var ele_text=document.createTextNode ("this is a text node")
CreateComment ()
CreateComment () is used to create a comment node, the node of nodeType=8.
Syntax:
Document.createComment (comment)
Where comment is the content of the comment and a node object is returned.
For example, create a comment node with the content "this is a comment node":
Var ele_comment=document.createComment ("this is a comment node")
CreateDocumentFragment ()
CreateDocumentFragment () is used to create the document fragment node.
A document fragment node is a collection of several DOM nodes, which can include various types of nodes, such as element nodes, text nodes, comment nodes, and so on. The document fragment node is empty at the beginning of creation and needs to be added to it.
Syntax:
Document.createDocumentFragment ()
For example, create a document fragment node and assign it to a variable:
Var ele_fragment=document.createDocumentFragment ()
The above is about the content of this article on "how to create nodes in Javascript". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.