最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - dynamic svg element added by Javascript doesn't work - Stack Overflow

programmeradmin0浏览0评论

I'm really confused here. I have a static SVG element that displays fine, but when I add an identical element from Javascript, it doesn't display. Why is this??

<html>
   <head>
	<script type="text/javascript">
	    function doit()
		{
			var svgdiv = document.getElementById('svg1');
			for (var k = 1; k < 3; ++k)
			{
				var svg = document.createElement('svg');
				svg.setAttribute('width',100);
				svg.setAttribute('height',100);
				console.log(svg);
				var c = document.createElement('circle');
				c.setAttribute('cx',50);
				c.setAttribute('cy',50);
				c.setAttribute('r',40);
				c.setAttribute('stroke','green');
				c.setAttribute('stroke-width',4);
				c.setAttribute('fill','yellow');
				svg.appendChild(c);
				svgdiv.appendChild(svg);
			}
		}
		window.onload = doit;
	</script>
  </head>
  <body>
	<svg width="100" height="100">
	  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
	</svg>
      <div id="svg1"></div>
   </body>
</html>

I'm really confused here. I have a static SVG element that displays fine, but when I add an identical element from Javascript, it doesn't display. Why is this??

<html>
   <head>
	<script type="text/javascript">
	    function doit()
		{
			var svgdiv = document.getElementById('svg1');
			for (var k = 1; k < 3; ++k)
			{
				var svg = document.createElement('svg');
				svg.setAttribute('width',100);
				svg.setAttribute('height',100);
				console.log(svg);
				var c = document.createElement('circle');
				c.setAttribute('cx',50);
				c.setAttribute('cy',50);
				c.setAttribute('r',40);
				c.setAttribute('stroke','green');
				c.setAttribute('stroke-width',4);
				c.setAttribute('fill','yellow');
				svg.appendChild(c);
				svgdiv.appendChild(svg);
			}
		}
		window.onload = doit;
	</script>
  </head>
  <body>
	<svg width="100" height="100">
	  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
	</svg>
      <div id="svg1"></div>
   </body>
</html>

Share Improve this question asked Dec 15, 2014 at 16:59 Jason SJason S 190k174 gold badges632 silver badges1k bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

Use

document.createElementNS('http://www.w3.org/2000/svg', 'svg')

instead of

document.createElement('svg')

Explanation:

SVG elements must be created in the SVG namespace and cannot therefore be created by createElement, instead you must use createElementNS providing the SVG namespace as the first argument.

createElement basically creates html elements called svg and circle rather than SVG elements.

text/html doesn't really have namespaces so the HTML parser magically switches to the SVG namespace when it encounters an <svg> element. If you changed the mime type to some XML namespace e.g. http://www.w3.org/1999/xhtml/ then you'd need an xmlns attribute on the root <html> element and also on the <svg> element.

<html>
   <head>
	<script type="text/javascript">
	    function doit()
		{
			var svgdiv = document.getElementById('svg1');
			for (var k = 1; k < 3; ++k)
			{
				var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
				svg.setAttribute('width',100);
				svg.setAttribute('height',100);
				console.log(svg);
				var c = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
				c.setAttribute('cx',50);
				c.setAttribute('cy',50);
				c.setAttribute('r',40);
				c.setAttribute('stroke','green');
				c.setAttribute('stroke-width',4);
				c.setAttribute('fill','yellow');
				svg.appendChild(c);
				svgdiv.appendChild(svg);
			}
		}
		window.onload = doit;
	</script>
  </head>
  <body>
	<svg width="100" height="100">
	  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
	</svg>
      <div id="svg1"></div>
   </body>
</html>

发布评论

评论列表(0)

  1. 暂无评论