代码
function FunNode(obj)
{
var objNode = document.getElementById("NodeID");
if(objNode!=null)
{
if(obj.checked)
{
var newNode = document.createElement("div");
newNode.id = "divId";
var newNodeText = document.createTextNode("Test Child");
newNode.appendChild(newNodeText);
objNode.parentNode.appendChild(newNode);
}
else
{
var n = document.getElementById("divId");
var wN =document.createTextNode("");
if(n!=null)
{
n.parentNode.replaceChild(wN,n);
}
}
}
}
{
var objNode = document.getElementById("NodeID");
if(objNode!=null)
{
if(obj.checked)
{
var newNode = document.createElement("div");
newNode.id = "divId";
var newNodeText = document.createTextNode("Test Child");
newNode.appendChild(newNodeText);
objNode.parentNode.appendChild(newNode);
}
else
{
var n = document.getElementById("divId");
var wN =document.createTextNode("");
if(n!=null)
{
n.parentNode.replaceChild(wN,n);
}
}
}
}
使用replaceChild()函数代替removeChild()来实现移除node.
n.parentNode.replaceChild(wN,n);