开发者

How to get handle to replaced child?

obj.parentNode.replaceChild(elem,obj)

Now I want to have a handle 开发者_运维知识库to that inserted node.


The "elem" handle is still valid even if it has been inserted into the DOM. So you can simply use elem to access the element that has replaced the old element.


Use the reference you already have: elem. That's the element you just added. The replaceChild call doesn't create any new nodes.


If you're doing something like this:

obj.parentNode.replaceChild(document.createElement('div'),obj)

...you'll not end up with any reference to the new element. You'll need to first retain it manually:

var elem = document.createElement('div');  // Create & reference the new element

obj.parentNode.replaceChild(elem,obj);  // Perform the replace

MDC Docs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜