javascript - 使用 jQuery 过滤标签

标签 javascript jquery filter svg

我有以下 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/

相关文章:

javascript - jQuery:如何将鼠标悬停事件放在由ajax生成的标签选择上

javascript - 使用特定类的链接填充 javascript 数组

javascript - 无法在 IE 8 及以下版本中使用 jQuery 解析文件,因为它不是 XML(即使它有点像 XML)

google-apps-script - 改为24小时。 DateRangeFilter 的模式

sharepoint - 如何使用文本字段过滤 SharePoint 列表?

javascript - Jade 模板一类

javascript - 基本的 coffeescript 查找是否在数组中

javascript - Django:Ajax 没有从服务器响应接收数据

javascript - 数组函数,其位置处的数字返回零

android - ffmpeg - 强制使用的过滤器