javascript 从变量设置 div 样式标签中的宽度和高度值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11040780/
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
Setting the width and heights value in div's style tag from variables
提问by goseib
In my javascript file I have the following line:
在我的 javascript 文件中,我有以下行:
<div id="mygraph" style="width:200px;height:100px;"></div>
I'd like to set the width and height values in the style tag from two variables that take their values from the corresponding functions:
我想从两个变量中设置样式标签中的宽度和高度值,这些变量从相应的函数中获取它们的值:
var my_width = getWidth();
var my_height = getHeight();
How can I accomplish that? I'm missing the correct syntax. Thank you in advance...
我怎样才能做到这一点?我缺少正确的语法。先感谢您...
回答by Zoltan Toth
The following assumes that my_height
and my_width
returned by your functions are plain numbers:
以下假设您的函数返回的my_height
和my_width
是普通数字:
document.getElementById('mygraph').style.height = my_height + "px";
document.getElementById('mygraph').style.width = my_width + "px";