简体   繁体   中英

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. 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. The console.log shows that the variable is set correctly. Even when I enter the assignment statement in the console of Chrome, it changes the height. There is an iframe inside of the div if that matters. 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 link 2

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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