Change height/width of object SVG using JavaScript

2019-04-19 11:34发布

问题:

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";        
}

回答1:

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

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

similarly for the width.