简体   繁体   English

用jQuery更改div的高度

[英]changing the height of a div with jquery

For some reason, when I try to change the height of a div using jquery, it will not work. 由于某些原因,当我尝试使用jquery更改div的高度时,它将无法工作。 The code I've used is this: 我使用的代码是这样的:

<div id="wrapdiv" style="height:2000px">
    ....
</div>

console.log(total);  // yields 1500
changediv = "wrapdiv";
$("#" + changediv).css('height', total);

No matter what I do, it will not change the height to 1500px. 无论我做什么,都不会将高度更改为1500px。 The console.log shows that the variable is set correctly. console.log显示变量已正确设置。 Even when I enter the assignment statement in the console of Chrome, it changes the height. 即使在Chrome的控制台中输入分配语句,它也会更改高度。 There is an iframe inside of the div if that matters. 如果这很重要,则div内有一个iframe。 Any ideas? 有任何想法吗?

You cannot simply put a numeric value only for the height. 您不能简单地只为高度输入数值。 You must do this: 您必须这样做:

<div id="wrapdiv" style="height:2000px">
    ....
</div>

console.log(total);  // yields 1500
changediv = "wrapdiv";
$("#" + changediv).css('height', total + 'px');

Or you can do: 或者,您可以执行以下操作:

<div id="wrapdiv" style="height:2000px">
    ....
</div>

console.log(total);  // yields 1500
changediv = "wrapdiv";
$("#" + changediv).height(total);

JSFiddle link 1 JSFiddle链接1

JSFiddle link 2 JSFiddle链接2

var total = 1500;
var changediv = "wrapdiv";

$("#" + changediv).height(total);
alert($("#" + changediv).height());

您应该已经寻找过Google ... ^^这是: http : //api.jquery.com/height/

$('#yourDiv').height(100);

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

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