一、定义
appendChild:插入一个子节点
insertBefore:在指定元素之前插入一个子节点
insertAfter:在指定元素后插入一个子节点
二、用法
appendChild
Element.appendChild(newChild)
newChild:插入的节点
在父元素Element插入一个子节点newChild
insertBefore
Element.insertBefore(newChild,Child)
newChild:插入的节点
Child:插入在此节点之前,可选参数(为空时此方法类似appendChild),另外火狐这个值不能为空,应该传个null进去。
在父元素Element插入一个子节点newChild,该子节点位于Child的前面
insertAfter
没有这个方法,我们可以自己写
function insertAfter(newnode,child){ //获取目标节点的父节点 var parent = child.parentNode; if(parent.lastChild == child){ parent.appendChid(newnode); }else{ parent.insertBefore(newnode,child.nextSibling); //nextSibling:下一个兄弟元素节点 } }