简体   繁体   English

如何根据div的外部数字更改div的尺寸? jQuery的/ CSS

[英]How to change the dimensions of a div depending of a number extern to that div? jquery/css

I want to change the dimensions of a div depending of wich number contain another div . 我想根据包含另一个div数字更改div的尺寸。 For example if i have the number 0 the div has to be width: 20px; height 0px; 例如,如果我的数字为0,则div必须为width: 20px; height 0px; width: 20px; height 0px; , if i have the number 50 the div has to be width: 20px; height 50px; ,如果我的数字为50,则div必须为width: 20px; height 50px; width: 20px; height 50px; , if i have the number 100 the div has to be width: 20px; height 100px; ,如果我的数字为100,则div必须为width: 20px; height 100px; width: 20px; height 100px; .

How can I do it in css or jquery? 如何在CSS或jquery中做到这一点?

I hope you understand. 我希望你明白。 Thank you. 谢谢。

Use css() function : 使用css()函数:

 $('#div-2').css("height", $('#div-1').text()+'px'); 
 #div-2{ background-color: green; width: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='div-1'>50</div> <div id='div-2'></div> 

Hope this helps. 希望这可以帮助。

Create a function to which on passing the width it will change your width to what you passed. 创建一个函数,将宽度传递到该函数时,宽度将更改为您传递的宽度。

function ChangeHeight(val){
   $('#div').css('height', val+'px');
}
ChangeHeight(100);

or simply do this if its just gonna be once per page lifetime. 或仅在每个页面生命周期内仅执行一次操作即可。

$('#div').css('height', val+'px');

This should do for you. 这应该为您做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM