[英]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>
cx
和cy
属性是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.