Loading SVG into SVGWeb dynamically with JQuery

前端 未结 2 1017
再見小時候
再見小時候 2021-02-08 18:53

I am trying to dynamically display some SVG content. This content is stored as a string in my data source. An example string would look like the following:



        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 19:46

    You can use jQuery SVG plugin The official website is down, but poke around for instructions.

    The relevant part you are asking is answer in this section, I believe..

    load(url, settings) this wrapper Load an external SVG document. Note that the browser may not allow you to load documents from sites other than that of the original page. If no callback is provided and an error arises, the error message is displayed within the SVG container.

    url (string) is the location of the SVG document or the actual SVG document inline.

    settings (boolean) see addTo below or (function) see onLoad below or (object) additional settings for the load with attributes below:

    addTo (boolean, optional) true to add to what's already there, or false (the default) to clear the canvas first changeSize (boolean, optional) true to allow the canvas size to change, or false (the default) to retain the original size onLoad (function, optional) callback after the document has loaded, this function receives the SVG wrapper object and an optional error message as parameters, and within which this is the SVG container division.

    svg.load('images/rect01.svg',
        {addTo: true, onLoad: loadDone});
    svg.load('...', loadDone);
    

    also, for kicks, here is some PHP code I used to insert such an SVG in a looping statement, that worked nicely..

    if (ICON_FILE_EXT == "svg") {
            print   "\svg.load(\'".ICON_PATH."/directory.svg\', \{addTo\: true, onLoad\: loadDone\}\)\;\<\/script\>";
    

提交回复
热议问题