【问题标题】:Creating SVG elements dynamically with javascript inside HTML在 HTML 中使用 javascript 动态创建 SVG 元素
【发布时间】:2013-12-30 14:38:27
【问题描述】:

我想在 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>

你能帮忙吗? 谢谢。

【问题讨论】:

标签: javascript html svg


【解决方案1】:

改变

var svg   = document.documentElement;

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

这样您就可以创建一个SVG 元素。

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

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

Demonstration

【讨论】:

  • 好的,我会的。文字有效,但我想改变它的位置。例如,我希望它显示在矩形中间的矩形上。
  • 请注意,该链接是一个 html 锚标记,如果您希望它位于 svg 内部,则需要创建一个 元素(在 svg 命名空间中)和一个 元素,然后将它们附加到 svg。参见例如stackoverflow.com/questions/19132443/…
【解决方案2】:

为了方便 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
}

现在你可以写了:

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

示例(具有改进的 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);

【讨论】:

  • 请注意,如果您尝试设置属性“viewBox”,“驼峰到破折号”功能会为您搞砸。
  • 你说得对,我们应该处理一些异常以使其正常运行
  • @chris 我们可以写"stroke-width":2tested it,效果很好。省略 v[p] 之前的 replace 并在属性名称中使用引号(如果其中有破折号)。混合不带引号的简单属性和带引号的破折号也可以。
【解决方案3】:

将此添加到 html:

<svg id="mySVG" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"/>

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

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);
}     

【讨论】:

    【解决方案4】:

    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);

    【讨论】:

    • 欢迎来到 Stack Overflow。没有任何解释的代码很少有帮助。 Stack Overflow 是关于学习的,而不是提供 sn-ps 来盲目复制和粘贴。请编辑您的问题并解释它如何回答所提出的具体问题。见How to Answer
    猜你喜欢
    • 2011-07-29
    • 2012-08-30
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多