繁体   English   中英

获取svg元素的位置

[英]Get position of svg element

为什么我无法获得cx和cy的值? 它打印一些数组。 我只需要2个值

 <!DOCTYPE html> <html> <body> <svg xmlns="http://www.w3.org/2000/svg"> <circle cx="100" cy="100" r="50" fill="red" id="cir"/> </svg> <script> console.log(document.getElementById("cir").cx); console.log(document.getElementById("cir").cy); </script> </body> </html> 

cxcy属性是SVGAnimatedLength对象,而不是字符串或数字。

要获取cx的实际值,您需要执行以下操作:

cx.baseVal.value

 <!DOCTYPE html> <html> <body> <svg xmlns="http://www.w3.org/2000/svg"> <circle cx="100" cy="100" r="50" fill="red" id="cir"/> </svg> <script> console.log(document.getElementById("cir").cx.baseVal.value); console.log(document.getElementById("cir").cy.baseVal.value); </script> </body> </html> 

使用console.log()而不是Console.log()

console是由浏览器定义的, Console不是(与​​大多数语言一样,JS区分大小写)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM