通过变量在div的样式标签中设置宽度和高度值 [英] Setting the width and heights value in div's style tag from variables

查看:117
本文介绍了通过变量在div的样式标签中设置宽度和高度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的javascript文件中,以下行:

In my javascript file I have the following line:

<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...

推荐答案

以下假设您的函数返回的 my_height my_width 是纯数字:

The following assumes that my_height and my_width returned by your functions are plain numbers:

document.getElementById('mygraph').style.height = my_height + "px";
document.getElementById('mygraph').style.width = my_width + "px";

这篇关于通过变量在div的样式标签中设置宽度和高度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆