如何使用常规 JavaScript 实现前置和附加? [英] How can I implement prepend and append with regular JavaScript?
问题描述
如何实现 prepend 和 在不使用 jQuery 的情况下使用常规 JavaScript 追加?
也许你在问 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)
.
这篇关于如何使用常规 JavaScript 实现前置和附加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!