Article directory
- append
- appendChild
- innerHTML
- the difference
- performance
- case
- createTextNode
- createElement
- getTime
append
MDN
The
Element.append
method inserts a set of Node objects or DOMString objects after the last child node of theElement
. The inserted DOMString object is equivalent to a Text node. Differences from Node.appendChild():
Element.append()
allows appending DOMString objects, while Node.appendChild() only accepts Node objects.
Element.append()
has no return value, whileNode.appendChild()
returns the added Node object.
Element.append()
can append multiple nodes and strings, butNode.appendChild()
can only append one node.
appendChild
w3school
The
appendChild()
method adds a node (element) as the last child to the element.
MDN
The
Node.appendChild()
method appends a node to the end of the list of children of the specified parent node. If the node to be inserted already exists in the document tree of the current document, thenappendChild()
will only move it from the original position to the new position (no need to remove the node to be moved beforehand ).
This means that it is impossible for a node to appear in different places in the document at the same time. So, if a node already has a parent, after being passed to this method, it will first be removed and then inserted in the new position. To keep a node already in the document, you can first use the Node.cloneNode() method to create a copy of it, and then attach the copy to the target parent node. Note that clones made withcloneNode
are not automatically kept in sync.
If the given child node is a DocumentFragment, then the entire contents of the DocumentFragment will be transferred to the list of children of the specified parent node.
A newerAPI
is available! > ParentNode.append() (en-US) method supports multiple parameters, accepts a string as a parameter, converts the string into a text node and appends it.
innerHTML
w3school
The
innerHTML
property sets or returns theHTML
content (innerHTML
) of an element.
MDN
The
Element.innerHTML
property sets or gets the descendant of the element in HTML syntax.
If a,or
node has a text child node that contains the characters
& amp;
,<
or>
,innerHTML
returns these characters as& amp;amp;
,& amp;lt;
, and& amp;gt;
. Use Node.textContent to obtain a correct copy of the content of these text nodes.
If you want to insert a piece ofHTML
into an element instead of replacing its content, then use the insertAdjacentHTML() method.Difference
append
can pass in multiple nodes or strings at the same time, and has no return value.
appendChild
can only pass one node, and does not directly support passing strings, you need to replace it withappendChild(document.createTextElement('string'))
, and return the appendedNode
node; if the parameter ofappendChild()
is an element existing on the page, the original element will be removed after execution.
WhatinnerHTML
adds is a pure string, and the properties of the inner element cannot be obtained; it is different from whatappendChild
adds to thedom
object, and what is returned is alsodom
object, you can access various attributes of the element through thedom
object.Performance
innerHTML
is more convenient thanappendChild
, especially when the created node has many attributes and contains text.
But in terms of execution speed, usingappendChild
is faster thaninnerHTML
, especially when the content includeshtml
tags,appendChild
Obviously faster thaninnerHTML
, this may be becauseinnerHTML
needs to parse the content before putting it on the page, when it containshtml<
innerHTML
will be significantly slower when there are too many /code> tags.Case list
html
<div id="test1"></div> <br> <input type="button" onclick="innerTest()" value="testInnerHTML"> <div id="test2"></div> <br> <input type="button" οnclick="appendTest()" value="testAppendChild">JavaScript
function innerTest() {<!-- --> let t1 = (new Date()).getTime(), t2 = undefined, a = "<b>apple</b>", b = document.getElementById("test1"); \t\t for (var i = 0; i < 500; i ++ ) b. innerHTML + = a; \t t2 = (new Date()).getTime(); \t console.log("testInnerHTML: " + (t2 - t1)); } function appendTest() {<!-- --> let t1 = (new Date()).getTime(), t2 = undefined, b = document.getElementById("test2"); \t\t for (var i = 0; i < 500; i ++ ) {<!-- --> let a = document. createElement("b"); \t\t a.appendChild(document.createTextNode("apple")); b. appendChild(a); } \t t2 = (new Date()).getTime(); \t console.log("testAppendChild: " + (t2 - t1)); }createTextNode
MDN
Creates a new text node. This method can be used to escape
HTML
characters.w3school
The
createTextNode()
method creates a text node.createElement
MDN
In an HTML document, the
Document.createElement
method is used to create anHTML
element specified by the tag nametagName
. If thetagName
is not recognized by the user agent, an unknownHTML
element HTMLUnknownElement is generated.w3school
The
createElement
method creates an element node.getTime
w3school
The
getTime()
method returns the time from midnight on1
of1
,1970
to the specified date milliseconds.MDN
The
getTime()
method returns the Greenwich Mean Time value of a time.
You can use this method to assign a datetime to another Date object. The function of this method is the same as the valueOf() method.