jquery's append not working with svg element?

后端 未结 14 2189
北海茫月
北海茫月 2020-11-21 06:28

Assuming this:



 
 

        
相关标签:
14条回答
  • 2020-11-21 06:56

    I can see circle in firefox, doing 2 things:

    1) Renaming file from html to xhtml

    2) Change script to

    <script type="text/javascript">
    $(document).ready(function(){
        var obj = document.createElementNS("http://www.w3.org/2000/svg", "circle");
        obj.setAttributeNS(null, "cx", 100);
        obj.setAttributeNS(null, "cy", 50);
        obj.setAttributeNS(null, "r",  40);
        obj.setAttributeNS(null, "stroke", "black");
        obj.setAttributeNS(null, "stroke-width", 2);
        obj.setAttributeNS(null, "fill", "red");
        $("svg")[0].appendChild(obj);
    });
    </script>
    
    0 讨论(0)
  • 2020-11-21 06:57

    The increasingly popular D3 library handles the oddities of appending/manipulating svg very nicely. You may want to consider using it as opposed to the jQuery hacks mentioned here.

    HTML

    <svg xmlns="http://www.w3.org/2000/svg"></svg>
    

    Javascript

    var circle = d3.select("svg").append("circle")
        .attr("r", "10")
        .attr("style", "fill:white;stroke:black;stroke-width:5");
    
    0 讨论(0)
  • 2020-11-21 06:58

    If the string you need to append is SVG and you add the proper namespace, you can parse the string as XML and append to the parent.

    var xml = jQuery.parseXML('<circle xmlns="http://www.w3.org/2000/svg" cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>');
    $("svg").append(xml.documentElement))
    
    0 讨论(0)
  • 2020-11-21 06:58

    A much simpler way is to just generate your SVG into a string, create a wrapper HTML element and insert the svg string into the HTML element using $("#wrapperElement").html(svgString). This works just fine in Chrome and Firefox.

    0 讨论(0)
  • 2020-11-21 07:01

    The accepted answer shows too complicated way. As Forresto claims in his answer, "it does seem to add them in the DOM explorer, but not on the screen" and the reason for this is different namespaces for html and svg.

    The easiest workaround is to "refresh" whole svg. After appending circle (or other elements), use this:

    $("body").html($("body").html());
    

    This does the trick. The circle is on the screen.

    Or if you want, use a container div:

    $("#cont").html($("#cont").html());
    

    And wrap your svg inside container div:

    <div id="cont">
        <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 100" width="200px" height="100px">
        </svg>
    </div>
    

    The functional example:
    http://jsbin.com/ejifab/1/edit

    The advantages of this technique:

    • you can edit existing svg (that is already in DOM), eg. created using Raphael or like in your example "hard coded" without scripting.
    • you can add complex element structures as strings eg. $('svg').prepend('<defs><marker></marker><mask></mask></defs>'); like you do in jQuery.
    • after the elements are appended and made visible on the screen using $("#cont").html($("#cont").html()); their attributes can be edited using jQuery.

    EDIT:

    The above technique works with "hard coded" or DOM manipulated ( = document.createElementNS etc.) SVG only. If Raphael is used for creating elements, (according to my tests) the linking between Raphael objects and SVG DOM is broken if $("#cont").html($("#cont").html()); is used. The workaround to this is not to use $("#cont").html($("#cont").html()); at all and instead of it use dummy SVG document.

    This dummy SVG is first a textual representation of SVG document and contains only elements that are needed. If we want eg. to add a filter element to Raphael document, the dummy could be something like <svg id="dummy" style="display:none"><defs><filter><!-- Filter definitons --></filter></defs></svg>. The textual representation is first converted to DOM using jQuery's $("body").append() method. And when the (filter) element is in DOM, it can be queried using standard jQuery methods and appended to the main SVG document which is created by Raphael.

    Why this dummy is needed? Why not to add a filter element strictly to Raphael created document? If you try it using eg. $("svg").append("<circle ... />"), it is created as html element and nothing is on screen as described in answers. But if the whole SVG document is appended, then the browser handles automatically the namespace conversion of all the elements in SVG document.

    An example enlighten the technique:

    // Add Raphael SVG document to container element
    var p = Raphael("cont", 200, 200);
    // Add id for easy access
    $(p.canvas).attr("id","p");
    // Textual representation of element(s) to be added
    var f = '<filter id="myfilter"><!-- filter definitions --></filter>';
    
    // Create dummy svg with filter definition 
    $("body").append('<svg id="dummy" style="display:none"><defs>' + f + '</defs></svg>');
    // Append filter definition to Raphael created svg
    $("#p defs").append($("#dummy filter"));
    // Remove dummy
    $("#dummy").remove();
    
    // Now we can create Raphael objects and add filters to them:
    var r = p.rect(10,10,100,100);
    $(r.node).attr("filter","url(#myfilter)");
    

    Full working demo of this technique is here: http://jsbin.com/ilinan/1/edit.

    ( I have (yet) no idea, why $("#cont").html($("#cont").html()); doesn't work when using Raphael. It would be very short hack. )

    0 讨论(0)
  • 2020-11-21 07:01
     var svg; // if you have variable declared and not assigned value.
     // then you make a mistake by appending elements to that before creating element    
     svg.appendChild(document.createElement("g"));
     // at some point you assign to svg
     svg = document.createElementNS('http://www.w3.org/2000/svg', "svg")
     // then you put it in DOM
     document.getElementById("myDiv").appendChild(svg)
     // it wont render unless you manually change myDiv DOM with DevTools
    
    // to fix assign before you append
    var svg = createElement("svg", [
        ["version", "1.2"],
        ["xmlns:xlink", "http://www.w3.org/1999/xlink"],
        ["aria-labelledby", "title"],
        ["role", "img"],
        ["class", "graph"]
    ]);
    function createElement(tag, attributeArr) {
          // .createElementNS  NS is must! Does not draw without
          let elem = document.createElementNS('http://www.w3.org/2000/svg', tag);             
          attributeArr.forEach(element => elem.setAttribute(element[0], element[1]));
          return elem;
    }
    // extra: <circle> for example requires attributes to render. Check if missing.
    
    0 讨论(0)
提交回复
热议问题