简体   繁体   中英

How to extract SVG element from external SVG file and append to HTML?

This question is similar, but the answer does not help.

The goal is to load an external SVG file (eg, https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg ), extract the SVG element, and append it to the HTML as an inline SVG element.

How can you do this?

We only want to use jQuery, no other third-party libraries.

If you have an svg element like this

<svg id="mySvgElement" width="100" height="100"></svg>

then, you can get an external svg file and put its content into your svg element like this:

$.get(externalSvgUrl, function(response){
    var content = $(response).html();
    $("#mySvgElement").html(content);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM