我有以下 SVG:
<g>
<rect id="1">
<text id="2">
<g>
<path id="3">
</g>
</g>
<text id="4">
<rect id="5">
我希望使用 jQuery 从集合中删除所有“g”标签,而保留所有其他内容(包括现在剥离的“g”标签中的标签)。
因此,我需要的 jQuery 集合如下所示:
<rect id="1">
<text id="2">
<path id="3">
<text id="4">
<rect id="5">
我试过使用过滤器,但无济于事(它不保留剥离的“g”标签的子标签):
$result = $svgCollection.filter('*:not(g)');
我以为 filter 充当了递归函数,但它似乎不起作用。也试过:
$result = $svgCollection.filter('text, rect, path');
但没有快乐。有什么想法吗?
最佳答案
$('g').each(
$(this).replaceWith($(this).contents())
});
或更短:
$('g').replaceWith(function() {
return $(this).contents()
});
关于javascript - 使用 jQuery 过滤标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7345745/