如何通过脚本来改变defs中定义的"use元素"的样式?我试图进入w3c工作草案接口,但我迷失在那个迷宫中
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="100"
height="100"
id="svg1">
<defs>
<g id="minchia" onclick="color(evt)">
<rect width="50" height="50" style="fill:#ffff6e;stroke:#ee1400;stroke-width:3" />
</g>
</defs>
<script type="text/javascript">
<![CDATA[
function color(evt)
{
node = evt.target.correspondingUseElement;
alert(node.getAttributeNS(null, "style")); /* empty! */
alert(node.getAttributeNS("http://www.w3.org/1999/xlink", "style")); /* empty! */
node.setAttributeNS("http://www.w3.org/1999/xlink", "fill", "blue"); /* nothing */
node.setAttributeNS(null, "fill", "blue"); /* nothing */
}
]]>
</script>
<use xlink:href="#minchia" id="frufru" x="10" y="10" />
</svg>
Run Code Online (Sandbox Code Playgroud)
更新
还有一件事:如果引用的元素是包含2个其他元素的"g",如rect和text,该怎么办?如何为正确的childNode设置属性(通过DOM方法)?在此示例中,setAttribute正在为引用元素的第一个子元素设置样式.如果我必须设计第二个样式怎么办?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="1000"
height="1000"
id="svg1">
<defs>
<g id="test" onclick="color(evt)" >
<rect
width="54"
height="58"
x="1.5"
y="1.5"
id="rect5" />
<text
x="-34"
y="47"
transform="matrix(0.66777386,-0.74436421,0.74436421,0.66777386,0,0)"
id="text2987"
style="font-size:30px;fill:#ffffff;stroke-width:0px">JC!</text>
</g>
</defs>
<script type="text/javascript">
<![CDATA[
function color(evt)
{
node = evt.target.correspondingUseElement;
node.setAttributeNS(null, "style", "fill:blue");
}
]]>
</script>
<use xlink:href="#test" id="frufru" x="10" y="10" style="fill:#000000" />
</svg>
Run Code Online (Sandbox Code Playgroud)
Phr*_*ogz 36
正如您在我的测试页面中看到的那样,如果您在<defs>文档部分中定义元素的视觉样式,则无法在<use>实例上覆盖该样式,而不是通过应用于该实例的可视属性,style属性或CSS类<use>.

此外,您不能在<use>元素上使用可视属性将样式级联到源的元素; 你必须使用CSS样式.
你必须:
使用CSS或设置手动解析或创建的style属性,例如
node.setAttribute('style','fill:blue');
Run Code Online (Sandbox Code Playgroud)由于这里要注意,你可以使用setAttribute(...)或setAttributeNS(null,...)SVG的属性.
更新:回答你的第二个问题:
如果引用的元素是包含2个其他元素的"g",如rect和text,该怎么办?
你不能使用CSS规则来选择a的伪子<use>; 他们根本就不存在.但是,你可以做的是应用你想要保留在其中的不变的样式,<def>然后应用style你想要的<use>.
例如:
<defs>
<g id="foo">
<!-- Every rect instance should be filled with blue -->
<rect width="54" height="58" x="1.5" y="1.5"
fill="blue" />
<!-- I want to be able to change text color per use
so I must be sure not to specify the fill style -->
<text x="-34" y="47" transform="matrix(0.668,-0.744,0.744,0.668,0,0)"
style="font-size:30px;stroke-width:0px">JC!</text>
</g>
</defs>
<use xlink:href="#foo" style="fill:orange" transform="translate(0,-100)" />
<use xlink:href="#foo" style="fill:yellow" transform="translate(0, 100)" />
Run Code Online (Sandbox Code Playgroud)
仅当您希望所有可更改项目的属性设置方式相同时,此方法才有效.
与HTML不同,SVG中的标记是演示文稿.我上面提到的是一些正常工作的hack,但通常<use>元素被设计为实例化定义的完整外观.如果您需要每个实例的自定义外观,也许您应该考虑克隆元素,修改属性,并将其添加到文档而不是黑客攻击<use>.