使用 JavaScript 更改对象 SVG 的高度/宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26392165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:50:08  来源:igfitidea点击:

Change height/width of object SVG using JavaScript

javascriptcsssvgheightwidth

提问by akm

I want change height or width when a click in a button. I try this but not work:

我想在单击按钮时更改高度或宽度。我尝试这个但不起作用:

function modify(){
    document.getElementById('cercle1').style.height = "10px";
    document.getElementById('cercle1').style.width = "10px";        
}

回答by Robert Longson

In SVG width and height of <rect>elements are attributes and not CSS properties so you'd need to write

在 SVG 中,<rect>元素的宽度和高度是属性而不是 CSS 属性,因此您需要编写

document.getElementById('cercle1').setAttribute("height", "10px");

similarly for the width.

宽度类似。