Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery调整div大小失败_Javascript_Jquery_Css - Fatal编程技术网

Javascript 使用jquery调整div大小失败

Javascript 使用jquery调整div大小失败,javascript,jquery,css,Javascript,Jquery,Css,我想用jquery调整我的div“container”的大小,我想我知道该怎么做了。但不幸的是,ist不起作用。以下是我的简单代码: var viewport = $( window ).height(); var container_height = viewport - 125; alert("setting height to "+container_height); $( "#container" ).height(container_height); alert("height is

我想用jquery调整我的div“container”的大小,我想我知道该怎么做了。但不幸的是,ist不起作用。以下是我的简单代码:

var viewport = $( window ).height();
var container_height = viewport - 125;
alert("setting height to "+container_height);
$( "#container" ).height(container_height);
alert("height is now " + $( "#container" ).height());
第一个警报告诉我正确的高度,但第二个警报告诉我,它是空的

怎么了?
谢谢

这对我很管用,试试这个:

$(window).on('resize', function(){

    var viewport = $( window ).height();

    var container_height = viewport - 125;

    $( "#container" ).height(container_height);
});
它在这里起作用

JS

 $(window).on('resize', function () {
    var viewport = $(window).height();
    var container_height = viewport - 125;
    alert("setting height to " + container_height);
    $("#container").height(container_height);
    alert("height is now " + $("#container").height());

});
#container{

    width:200px;
    height:200px;
    background:Yellow;
}
 $(window).on('resize', function () {
    var viewport = $(window).height();
    var container_height = viewport - 125;
    alert("setting height to " + container_height);
    $("#container").height(container_height);
    alert("height is now " + $("#container").height());

});