更新时间:2022-11-19 18:47:36
也许你在问 DOM 方法 appendChild
和 insertBefore
.
parentNode.insertBefore(newChild, refChild)
将节点 newChild
作为 parentNode
的子节点插入到现有的子节点 refChild
.(返回 newChild
.)
如果refChild
为空,newChild
被添加到列表的末尾孩子们.同样,更易读,使用parentNode.appendChild(newChild)
.
How can I implement prepend and append with regular JavaScript without using jQuery?
Perhaps you're asking about the DOM methods appendChild
and insertBefore
.
parentNode.insertBefore(newChild, refChild)
Inserts the node
newChild
as a child ofparentNode
before the existing child noderefChild
. (ReturnsnewChild
.)If
refChild
is null,newChild
is added at the end of the list of children. Equivalently, and more readably, useparentNode.appendChild(newChild)
.