Creating SVG elements dynamically with javascript inside HTML

前端 未结 3 1519
轮回少年
轮回少年 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:50

    Change

    var svg   = document.documentElement;
    

    to

    var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
    

    so that you create a SVG element.

    For the link to be an hyperlink, simply add a href attribute :

    h.setAttributeNS(null, 'href', 'http://www.google.com');
    

    Demonstration

提交回复
热议问题