我可以在javascript中获取对象svg的宽度,并将值保存在变量中。这是我的对象
<rect id="cercle1" x="5" y="25" width="50" height="50" stroke-width="1" stroke="#0E0E0E" style="fill:red; stroke:black; stroke-width:2"; />
Run Code Online (Sandbox Code Playgroud)
我尝试这样:
document.getElementById('cercle1')。width.value;
感谢帮助
您可以使用getAttribute函数:
document.getElementById('cercle1').getAttribute("width");
Run Code Online (Sandbox Code Playgroud)
这将使您从HTML代码中获得字符串。正如Phrogz所说,您可能需要一个数字或实际值(可能有所不同)。如果是这样,请参考他的答案。
该width属性是一个SVGAnimatedLength属性。因此,您需要:
// If you want the original value
var w = document.getElementById('cercle1').width.baseVal.value;
// If it is animated and you want the current animated value
var w = document.getElementById('cercle1').width.animVal.value;
Run Code Online (Sandbox Code Playgroud)
或者,如果只想要源代码中的内容:
var w = document.getElementById('cercle1').getAttribute('width')*1; // convert to num
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6706 次 |
| 最近记录: |