为什么我无法获取 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>
关于javascript - 获取svg元素的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47016584/