admin管理员组文章数量:1332373
I am trying to remove a node from the dom tree using javascript while keeping it's children. I tested the 3 approaches shown below and they work fine in Firefox but do not in IE 8 (see example below).
function removeNodeKeepChildren(node){
// approach 1
jQuery(node.parentNode).children().map(function (index) {
jQuery(this).replaceWith(jQuery(this).contents());
});
// approach 2
// doesn't work with content() either
jQuery(node.parentNode).html(jQuery(node).html());
// approach 3
var childfragment = document.createDocumentFragment();
for(var i=0; i<node.childNodes.length;i++){
childfragment.appendChild((node.childNodes[i]).cloneNode());
}
node.parentNode.replaceChild(childfragment,node);
}
Example input node:
<span class="A1">
start text
<span class="F">
bold text
</span>
end text
</span>
what it should result in:
start text
<span class="F">
bold text
</span>
end text
What IE 8 does:
start text
<span class="F">
</span>
end text
As you can see IE ignores/removes nested children.
I'd appreciate any help :)
I am trying to remove a node from the dom tree using javascript while keeping it's children. I tested the 3 approaches shown below and they work fine in Firefox but do not in IE 8 (see example below).
function removeNodeKeepChildren(node){
// approach 1
jQuery(node.parentNode).children().map(function (index) {
jQuery(this).replaceWith(jQuery(this).contents());
});
// approach 2
// doesn't work with content() either
jQuery(node.parentNode).html(jQuery(node).html());
// approach 3
var childfragment = document.createDocumentFragment();
for(var i=0; i<node.childNodes.length;i++){
childfragment.appendChild((node.childNodes[i]).cloneNode());
}
node.parentNode.replaceChild(childfragment,node);
}
Example input node:
<span class="A1">
start text
<span class="F">
bold text
</span>
end text
</span>
what it should result in:
start text
<span class="F">
bold text
</span>
end text
What IE 8 does:
start text
<span class="F">
</span>
end text
As you can see IE ignores/removes nested children.
I'd appreciate any help :)
Share Improve this question asked Sep 24, 2012 at 8:47 sotixsotix 8222 gold badges8 silver badges24 bronze badges 2-
What's the problem with this approach
http://jsfiddle/NKpMC/
. Its working fine here – Jashwant Commented Sep 24, 2012 at 8:59 - You are right. I spotted another error in my code which prevented it from working. Thanks for your help :) – sotix Commented Sep 24, 2012 at 9:44
4 Answers
Reset to default 3It should be easy to do like this:
function removeKeepChildren(node) {
var $node = $(node);
$node.contents().each(function() {
$(this).insertBefore($node);
});
$node.remove();
}
See it in action.
Use unwrap(), that's what it's intended for.
<span class="A1">
start text
<span class="F">
bold text
</span>
end text
</span>
<script>
jQuery(function($){$('.F').unwrap();});
</script>
Next is most simplest and fastest native javascript code:
function removeNodeKeepChildren(node) {
if (!node.parentElement) return;
while(node.firstChild)
{
node.parentElement.insertBefore(node.firstChild, node);
}
node.parentElement.removeChild(node);
}
http://jsfiddle/N3J7K/
@Jon's approach, sans iteration:
function removeKeepChildren(node) {
var $node = $(node);
var contents = $node.contents();
$node.replaceWith(contents);
}
See it in action.
@Dr.Molle's answer should be the accepted one.
本文标签: javascriptIE 8 remove node keep childrenStack Overflow
版权声明:本文标题:javascript - IE 8: remove node keep children - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742309630a2450605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论