function insertAfter(newElement,targetElement){
var parent=targetElement.parentNode;
if(parent.lastChild==targetElement){
parent.appendChild(newElement);
}else{
parent.insertBefore(newElement,targetElement.nextSibling);
}
}
注:newElement=document.getElementById("id");
targetElement为刚创建的节点。例:
var para=document.createElement("p");
var para.setAttribute("id","desription");(等价于var testdiv=document.getElementById("description");testdiv.appendChild(para);)
var text=document.createTextNode("Hello world");
para.appendChila(text);
newElement=para;
时间: 2024-10-09 22:07:50