簡體   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