使用Javascript在SVG中追加路径子

Inf*_*tor 25 javascript svg

美好的一天,

我在创建路径并使用SVG元素中的"appendChild"显示它时遇到了难以置信的困难.

我必须遗漏一些非常简单的东西,因为我已经倾注了W3C的规格,w3schools.com,各种帖子以及各种方式尝试忍者谷歌.

我在FireFox和Chrome中进行测试.

我有一个简单的svg文件:

<svg xmlns:svg ... id="fullPageID">
 <image id="timer1" x="100" y="100" width="100px" height="100px" xlink:href="../component/timer/timer1.svg" />  
 <image id="battery1" x="200" y="200" width="100px" height="100px" xlink:href="../component/battery/30x20_ochre.svg" />
 <script xlink:href="pathMaker.js" type="text/javascript" id="pathMaker" />  
</svg>
Run Code Online (Sandbox Code Playgroud)

我试图使用的脚本如下:

newpath = document.createElementNS("SVG","path");  
newpath.setAttribute("id", "pathIdD");  
newpath.setAttribute("d", "M 1,97.857143 C 19.285714,96.428571 24.016862,131.64801 90.714286,132.85714 140.78762,133.7649 202.79376,66.16041 202.79376,66.16041");  
newpath.setAttribute("stroke", "black");  
newpath.setAttribute("stroke-width", 3);  
newpath.setAttribute("opacity", 1);  
newpath.setAttribute("fill", "none");

document.getElementById("fullPageID").appendChild(newpath);
Run Code Online (Sandbox Code Playgroud)

我只是想做一些简单的工作.我错误地认为我不需要像在Library中使用Javascript生成SVG路径那样复杂的解决方案吗?

谢谢你.

Ant*_*ony 31

有两个问题:

  1. 正如已经指出的那样,你必须使用完整的命名空间uri,所以在这种情况下:

    newpath = document.createElementNS('http://www.w3.org/2000/svg',"path");  
    
    Run Code Online (Sandbox Code Playgroud)
  2. 还需要在设置名称空间时设置属性.好消息是你可以null作为命名空间uri 传入,所以:

    newpath.setAttributeNS(null, "d", "M 1,97.857143 C 19.285714,96.428571 24.016862,131.64801 90.714286,132.85714 140.78762,133.7649 202.79376,66.16041 202.79376,66.16041");
    
    Run Code Online (Sandbox Code Playgroud)

此外,这里有两种方法可以更轻松地处理svg命名空间(假设它是一个独立的svg,而不是嵌入在HTML中):

  • 要引用svg元素,而不是给它一个ID,你可以使用document.rootElement.
  • document.rootElement.getAttribute(null, "xmlns")返回一个空字符串(虽然请求其他属性确实可以使用此方法.请使用document.rootElement.namespaceURI.

因此,在您的代码中,您可以进行以下重写:

从:

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

至:

 newpath = document.createElementNS(document.rootElement.namespaceURI,"path");  
Run Code Online (Sandbox Code Playgroud)

要附加元素,您可以从:

document.getElementById("fullPageID").appendChild(newpath);
Run Code Online (Sandbox Code Playgroud)

至:

document.rootElement.appendChild(newpath);
Run Code Online (Sandbox Code Playgroud)

所以最后的脚本将是:

newpath = document.createElementNS(document.rootElement.namespaceURI,"path");  
newpath.setAttributeNS(null, "id", "pathIdD");  
newpath.setAttributeNS(null, "d", "M 1,97.857143 C 19.285714,96.428571 24.016862,131.64801 90.714286,132.85714 140.78762,133.7649 202.79376,66.16041 202.79376,66.16041");  
newpath.setAttributeNS(null, "stroke", "black"); 
newpath.setAttributeNS(null, "stroke-width", 3);  
newpath.setAttributeNS(null, "opacity", 1);  
newpath.setAttributeNS(null, "fill", "none");

document.rootElement.appendChild(newpath);
Run Code Online (Sandbox Code Playgroud)

  • #2不正确; 使用`setAttribute(a,b)`相当于`setAttributeNS(null,a,b)`:它的工作原理也一样. (4认同)
  • #2是不正确的,因为它是使用较新的DOM规范添加非html属性的"官方"方式.我遇到了Chrome的问题,其中属性存在于源检查器中,但未编译到样式中.即使它有效,也是因为WebKit和Gecko允许它兼容,而不是因为它真正等同.重点是:如果您不希望脚本在某个时刻中断,最好使用预期的方法,而不是那种适用于传统校对的方法.我确信在某些时候`createElement`也适用于那些引擎. (2认同)

Ste*_*sen 4

调用中的命名空间需要为“http://www.w3.org/2000/svg”,而不是“SVG” createElementNS