javascript - 如何将 SVG 元素从 String 添加到 DOM

标签 javascript svg

我想将包含 rect 的 SVG 添加到 DOM,并使用字符串中的标签。

我的工作方式不太好。

		var documentAsString =
		'<?xml version="1.0" encoding="UTF-8"?>\
			<document xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\
				<svg id="container" >\
					<g xmlns:xlink="http://www.w3.org/1999/xlink">\
						<rect x="50" y="50" width="50" height="50" fill="red"/>\
						<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#shape" x="200" y="50" fill="blue"></use>\
					</g>\
				</svg>\
			</document>\
		';
		
		var newDocument = (new DOMParser()).parseFromString(documentAsString, "text/xml");
		var container = newDocument.getElementById("container");
		var useContainer = document.getElementById('use-container');

		useContainer.removeChild(useContainer.firstElementChild);
		useContainer.appendChild(container.getElementsByTagName('g')[0]);
	<svg>
		<defs>
			<g id="shape">
				<rect x="50" y="50" width="50" height="50" />
				<circle cx="50" cy="50" r="50" />
			</g>
		</defs>

		<use xlink:href="#shape" x="50" y="50" />
		<g id="use-container" xmlns:xlink="http://www.w3.org/1999/xlink">
			<rect x="10" y="10" width="50" height="50" fill="red"></rect>
		</g>
	</svg>

最佳答案

SVG 元素需要位于 svg 命名空间中,即具有具有适当值的 xmlns 属性...

		var documentAsString =
		'<?xml version="1.0" encoding="UTF-8"?>\
			<document xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\
				<svg xmlns="http://www.w3.org/2000/svg" id="container" >\
					<g xmlns:xlink="http://www.w3.org/1999/xlink">\
						<rect x="50" y="50" width="50" height="50" fill="red"/>\
						<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#shape" x="200" y="50" fill="blue"></use>\
					</g>\
				</svg>\
			</document>\
		';
		
		var newDocument = (new DOMParser()).parseFromString(documentAsString, "text/xml");
		var container = newDocument.getElementById("container");
		var useContainer = document.getElementById('use-container');

		useContainer.removeChild(useContainer.firstElementChild);
		useContainer.appendChild(container.getElementsByTagName('g')[0]);
	<svg>
		<defs>
			<g id="shape">
				<rect x="50" y="50" width="50" height="50" />
				<circle cx="50" cy="50" r="50" />
			</g>
		</defs>

		<use xlink:href="#shape" x="50" y="50" />
		<g id="use-container" xmlns:xlink="http://www.w3.org/1999/xlink">
			<rect x="10" y="10" width="50" height="50" fill="red"></rect>
		</g>
	</svg>

关于javascript - 如何将 SVG 元素从 String 添加到 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32722224/

相关文章:

javascript - 清除 map 是否有助于垃圾收集?

html - tomcat6 上的 webapp 中的 SVG 在 IE9 中不起作用

javascript - 我正确处理 SVG 了吗?

javascript - 适用于 Window Chrome 和 SEO 的最新 Font-Smooth 技术

html - 动画在 mozilla 中不起作用

javascript - Rails Angular 和实时更新

javascript - 是否有任何用于构建像 johnpapa/angular-styleguide 这样的 ionic 应用程序的样式指南

javascript - 使用jquery移动相机效果

javascript - svg 动画动画不流畅

javascript - 如何向此 JS PNG 转换器添加更多列? [ Highcharts ]