且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用常规 JavaScript 实现前置和附加?

更新时间:2022-11-19 18:47:36

也许你在问 DOM 方法 appendChildinsertBefore.

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 of parentNode before the existing child node refChild. (Returns newChild.)

If refChild is null, newChild is added at the end of the list of children. Equivalently, and more readably, use parentNode.appendChild(newChild).