Creating SVG elements dynamically with javascript inside HTML

前端 未结 3 1511
轮回少年
轮回少年 2020-11-28 20:33

I want to create a rectangle inside an HTML page, then write some text on that rectangle. I also need that text to be a hyperlink. This is what I did but it is not working:<

3条回答
  •  渐次进展
    2020-11-28 20:48

    Add this to html:

    
    

    Try this function and adapt for you program:

    var svgNS = "http://www.w3.org/2000/svg";  
    
    function createCircle()
    {
        var myCircle = document.createElementNS(svgNS,"circle"); //to create a circle. for rectangle use "rectangle"
        myCircle.setAttributeNS(null,"id","mycircle");
        myCircle.setAttributeNS(null,"cx",100);
        myCircle.setAttributeNS(null,"cy",100);
        myCircle.setAttributeNS(null,"r",50);
        myCircle.setAttributeNS(null,"fill","black");
        myCircle.setAttributeNS(null,"stroke","none");
    
        document.getElementById("mySVG").appendChild(myCircle);
    }     
    

提交回复
热议问题