如何使用常规 JavaScript 实现前置和附加? [英] How can I implement prepend and append with regular JavaScript?

查看:29
本文介绍了如何使用常规 JavaScript 实现前置和附加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现 prepend在不使用 jQuery 的情况下使用常规 JavaScript 追加?

解决方案

也许你在问 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).

这篇关于如何使用常规 JavaScript 实现前置和附加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆