代码之家  ›  专栏  ›  技术社区  ›  Crashalot

jQuery:从具有多个节点的HTML字符串中提取最顶层的SVG元素

  •  0
  • Crashalot  · 技术社区  · 6 年前

    This question this question

    目标是从HTML字符串中提取最顶层的SVG元素。

    $(htmlString).find("svg") 不起作用。

    $($.parseHTML(htmlString)) htmlString 在一个jQuery对象中,可以执行查找并检索最顶层的SVG元素。

    HTML字符串示例:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <!-- Generator: Adobe Illustrator 18.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 424 424" style="enable-background:new 0 0 424 424;" xml:space="preserve" width="512px" height="512px">
    <g>
        <g>
            <path d="M35,89C15,89,0,74,0,54s15-36,35-36h353c20,0,36,16,36,36s-16,35-36,35H35z" fill="#7c7a7d"/>
            <path d="M388,176c20,0,36,16,36,36s-16,35-36,35H35c-20,0-35-15-35-35s15-36,35-36H388z" fill="#7c7a7d"/>
            <path d="M388,335c20,0,36,15,36,35s-16,36-36,36H35c-20,0-35-16-35-36s15-35,35-35H388z" fill="#7c7a7d"/>
        </g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    </svg>
    

    $($.parseHTML(htmlString))的结果,其中htmlString是上面的字符串:

    $($.parseHTML(htmlString))
    
    w.fn.init(6) [comment, text, comment, text, svg#Capa_1, text]
    0: comment
    1: text
    2: comment
    3: text
    4: svg#Capa_1
    5: text
    length: 6
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   Kaiido NickSlash    6 年前

    你不能使用 find 因为您给jQuery的标记并不表示树:Doctype和comments节点是您的同级节点 <svg> .

    filter 为了只保留svg节点,jQuery条目如下:

    const $svg = $(`<?xml version="1.0" encoding="utf-8"?>
    <!-- comment1  -->
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 424 424">
      <g>
        <g>
          <path d="M35,89C15,89,0,74,0,54s15-36,35-36h353c20,0,36,16,36,36s-16,35-36,35H35z" fill="#7c7a7d"/>
          <path d="M388,176c20,0,36,16,36,36s-16,35-36,35H35c-20,0-35-15-35-35s15-36,35-36H388z" fill="#7c7a7d"/>
          <path d="M388,335c20,0,36,15,36,35s-16,36-36,36H35c-20,0-35-16-35-36s15-35,35-35H388z" fill="#7c7a7d"/>
        </g>
      </g>
    </svg>`)
    .filter((i, el) => $(el).is('svg'));
    
    console.log($svg.attr('id'));
    $svg.find('path').attr('fill', 'red');
    $('body').append($svg);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    documentElement 将是你的 <svg> 然后,您将能够从jQuery处理它:

    const str = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 424 424">
      <g>
        <g>
          <path d="M35,89C15,89,0,74,0,54s15-36,35-36h353c20,0,36,16,36,36s-16,35-36,35H35z" fill="#7c7a7d"/>
          <path d="M388,176c20,0,36,16,36,36s-16,35-36,35H35c-20,0-35-15-35-35s15-36,35-36H388z" fill="#7c7a7d"/>
          <path d="M388,335c20,0,36,15,36,35s-16,36-36,36H35c-20,0-35-16-35-36s15-35,35-35H388z" fill="#7c7a7d"/>
        </g>
      </g>
    </svg>`;
    
    const doc = (new DOMParser).parseFromString(str, 'image/svg+xml');
    // use the second argument (context) of jQuery()
    const $svg = $('svg', doc);
    console.log($svg.attr('id'));
    $svg.find('path').attr('fill', 'red');
    $('body').append($svg);
    <脚本src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js“></脚本>