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
精彩评论