How to access SVG elements with Javascript

后端 未结 3 1801
野的像风
野的像风 2020-11-22 07:54

I\'m messing around with SVG and I was hoping I could create SVG files in Illustrator and access elements with Javascript.

Here\'s the SVG file Illustrator kicks ou

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 08:29

    In case you use jQuery you need to wait for $(window).load, because the embedded SVG document might not be yet loaded at $(document).ready

    $(window).load(function () {
    
        //alert("Document loaded, including graphics and embedded documents (like SVG)");
        var a = document.getElementById("alphasvg");
    
        //get the inner DOM of alpha.svg
        var svgDoc = a.contentDocument;
    
        //get the inner element by id
        var delta = svgDoc.getElementById("delta");
        delta.addEventListener("mousedown", function(){ alert('hello world!')}, false);
    });
    

提交回复
热议问题