我想通过单击按钮来更改应用于 SVG 的类。代码是here http://jsfiddle.net/p19rnmev/ .
我的 SVG 看起来像:
<svg id="test" class="fill" xmlns="http://www.w3.org/2000/svg" width="100%" height="200px" >
<path id="path22276" d="m500 81c-41-6-110-35-110-46 0-6 32 6 48 19 8 5 53 12 101 14 78 4 93 1 144-22 32-14 60-26 64-26 8 0-37 34-62 47-28 15-131 22-185 14z"/>
</svg>
我尝试了以下代码:
document.getElementById("test").className = "halffill";
document.querySelector("svg.fill").setAttribute("class","halffill");
我针对普通 div 标签测试了相同的 javascript 代码。效果很好。 SVG 有一些限制吗?请帮我解决这个问题。
The className
SVG 元素上的属性与className
HTML 元素的属性。
在 HTML 中,className
是一个字符串。在 SVG 中它是一个SVGAnimatedString
目的。这就是为什么你不能这样做svgElement.className = "halffill"
.
但是,第二行(使用setAttribute()
) 应该管用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)