我想改变一个SVG对象的属性。用JS处理对象
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:xlink="http://www.w3.org/1999/xlink">
<head>
<title>SVG use test</title>
<script type="text/javascript">
function setUsedFill(uId, fill) {
document.getElementById(uId).instanceRoot.correspondingElement.setAttributeNS(null, 'fill', fill);
}
</script>
</head>
<body>
<div>
<input type="button" value="test" onclick="setUsedFill('uc1', 'yellow');"/>
<input type="button" value="test" onclick="setUsedFill('uc2', 'red');"/>
</div>
<div>
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300">
<defs>
<circle id="c1" cx="50" cy="50" r="30" fill="green"/>
</defs>
<use id="uc1" x="0" y="0" xlink:href="#c1"></use>
<use id="uc2" x="100" y="100" xlink:href="#c1"></use>
</svg>
</div>
</body>
</html>
此代码是怎么回事在Opera,Chrome和IE 9
instanceRoot.correspondingElement
- 是不是在Firefox/Mozilla的
注意SVG2已删除从'SVGUseElement'了'instanceRoot'访问,看到https://svgwg.org/svg2-draft/changes。 HTML#结构。 –