使用HTML中的JavaScript动态创建SVG元素

use*_*087 56 html javascript svg

我想在HTML页面内创建一个矩形,然后在该矩形上写一些文本.我还需要该文本作为超链接.这就是我做的,但它不起作用:

    <!DOCTYPE html>
<html>
<body>

<script>

    var svg   = document.documentElement;
    var svgNS = svg.namespaceURI;

    var rect = document.createElementNS(svgNS,'rect');
    rect.setAttribute('x',5);
    rect.setAttribute('y',5);
    rect.setAttribute('width',500);
    rect.setAttribute('height',500);
    rect.setAttribute('fill','#95B3D7');
    svg.appendChild(rect);
    document.body.appendChild(svg);

    var h=document.createElement('a');
    var t=document.createTextNode('Hello World');
    h.appendChild(t);
    document.body.appendChild(h);


</script>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

你能帮帮忙吗?谢谢.

Den*_*ret 93

更改

var svg   = document.documentElement;
Run Code Online (Sandbox Code Playgroud)

var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
Run Code Online (Sandbox Code Playgroud)

这样你就可以创建一个SVG元素.

要使链接成为超链接,只需添加一个href属性:

h.setAttributeNS(null, 'href', 'http://www.google.com');
Run Code Online (Sandbox Code Playgroud)

示范


小智 33

将此添加到html:

<svg id="mySVG" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"/>
Run Code Online (Sandbox Code Playgroud)

试试这个功能并适应你的程序:

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);
}     
Run Code Online (Sandbox Code Playgroud)


Jos*_*nac 28

为了方便svg编辑,您可以使用中间函数:

function getNode(n, v) {
  n = document.createElementNS("http://www.w3.org/2000/svg", n);
  for (var p in v)
    n.setAttributeNS(null, p, v[p]);
  return n
}
Run Code Online (Sandbox Code Playgroud)

现在你可以写:

svg.appendChild( getNode('rect', { width:200, height:20, fill:'#ff0000' }) );
Run Code Online (Sandbox Code Playgroud)

示例(使用改进的getNode函数,允许使用短划线的属性,例如strokeWidth> stroke-width):

function getNode(n, v) {
  n = document.createElementNS("http://www.w3.org/2000/svg", n);
  for (var p in v)
    n.setAttributeNS(null, p.replace(/[A-Z]/g, function(m, p, o, s) { return "-" + m.toLowerCase(); }), v[p]);
  return n
}

var svg = getNode("svg");
document.body.appendChild(svg);

var r = getNode('rect', { x: 10, y: 10, width: 100, height: 20, fill:'#ff00ff' });
svg.appendChild(r);

var r = getNode('rect', { x: 20, y: 40, width: 100, height: 40, rx: 8, ry: 8, fill: 'pink', stroke:'purple', strokeWidth:7 });
svg.appendChild(r);
Run Code Online (Sandbox Code Playgroud)

  • @chris 我们可以只写 `"stroke-width":2`,[tested it](https://jsfiddle.net/ht6phnt9/2/),效果很好。省略 v[p] 之前的 `replace` 并在属性名称中使用引号(如果其中有破折号)。混合不带引号的简单属性和带引号的破折号也可以。 (4认同)
  • 请注意,如果您尝试设置属性“viewBox”,“驼峰式到破折号功能”会为您搞砸。 (2认同)