javascript替换子项不工作

javascript替换子项不工作,javascript,Javascript,我知道这是一个简单的概念,但我似乎无法让它工作。你知道为什么吗?这是我的密码: var oldChild = document.getElementById("sbX1"); var newChild = document.createElement("div"); newChild.id= "sbYY1"; oldChild.replaceChild(newChild, oldChild); 您在oldchild上调用replaceChild,而您应该在oldchild的父节点上调用它 va

我知道这是一个简单的概念,但我似乎无法让它工作。你知道为什么吗?这是我的密码:

var oldChild = document.getElementById("sbX1");
var newChild = document.createElement("div");
newChild.id= "sbYY1";
oldChild.replaceChild(newChild, oldChild);
您在oldchild上调用replaceChild,而您应该在oldchild的父节点上调用它

var oldChild = document.getElementById("sbX1");
var newChild = document.createElement("div");
newChild.id= "sbYY1";

// Replace oldchild on the parent node
// You can reference the child's parent via .parentNode
// or retrieve it directly with document.getElementById('theparentId')
oldchild.parentNode.replaceChild(newChild, oldChild);