【发布时间】:2012-09-14 20:38:17
【问题描述】:
在树中间插入节点的最推荐/最有效的方法是什么。
如何转置这个
<svg id="root" ... >
<g id="child1">...</g>
<text id="child2">...</text>
<rect id="child3">...</rect>
...
</svg>
到这里
<svg id="root" ... >
<g id="parent">
<g id="child1">...</g>
<text id="child2">...</text>
<rect id="child3">...</rect>
...
</g>
</svg>
我试过了
var $parent = $("g").attr("id","parent");
var $root = $("#root");
$root.contents().each(function(){
$child=$(this);
$child.remove();
$parent.append($child);
});
$root.append($parent);
我也试过在this答案中使用moveTo方法
(function($){
$.fn.moveTo = function(selector){
return this.each(function(){
var cl = $(this).clone();
$(cl).appendTo(selector);
$(this).remove();
});
};
})(jQuery);
$root.contents().each(function() {
$(this).moveTo($parent);
});
所有这些方法都适用于简单的场景,但是当树真的很大时,浏览器就会挂起,有没有更有效的方法来执行此操作?
我正在寻找 jQuery 或纯 javascript 解决方案。
【问题讨论】:
标签: javascript jquery dom svg dom-manipulation