最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript replacechild not working - Stack Overflow

programmeradmin2浏览0评论

I know this is an easy concept, but I just can't seem to get it working. Any ideas why? Here is my code:

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

I know this is an easy concept, but I just can't seem to get it working. Any ideas why? Here is my code:

var oldChild = document.getElementById("sbX1");
var newChild = document.createElement("div");
newChild.id= "sbYY1";
oldChild.replaceChild(newChild, oldChild);
Share Improve this question asked Jun 12, 2012 at 0:28 DanielDaniel 4,34212 gold badges51 silver badges69 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You're calling replaceChild() on oldchild, when you ought to be calling it on the parent node of 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);
发布评论

评论列表(0)

  1. 暂无评论