使用jquery实现DIV的100%高度-如何使其与父级关联

使用jquery实现DIV的100%高度-如何使其与父级关联,jquery,height,Jquery,Height,我使用这个jquery脚本100%扩展一个子DIV。它工作正常,但DIV超出了浏览器窗口,因此我必须始终向下滚动。是否可以将100%高度更改为与父分区相关,而不是与文档相关 $(document).ready(function () { $("#stretch").animate({ height: $(document).height() }, 500 ); }); 试一试 或者,如果不是imeditate父级: $(document).ready(function () {

我使用这个jquery脚本100%扩展一个子DIV。它工作正常,但DIV超出了浏览器窗口,因此我必须始终向下滚动。是否可以将100%高度更改为与父分区相关,而不是与文档相关

$(document).ready(function () {
    $("#stretch").animate({ height: $(document).height() }, 500 );
});
试一试

或者,如果不是imeditate父级:

$(document).ready(function () {
    var $stretch = $("#stretch");
    $('#stretch').animate({ height: $stretch.closest('parent-selector').height() }, 500 );
});
当然是:

直接家长:

$(document).ready(function () {
    var $stretch = $("#stretch");

    $stretch.animate({ height: $stretch.parent().height() }, 500 );
});


如果它不是直接的父对象,您可以用
最近的
替换
父对象

丢失的DIV-不出现。这实际上不起作用,因为
在此上下文中不是元素。请找一个粘贴站并粘贴html。问题是您没有在父对象上设置高度!你能不能#框体{width:771px;margin:0 auto;height:500px;}然后你能详细解释一下“no bueno”?我的父母是:#body_wrap{width:100%;min height:100%;height:auto!重要;height:100%;margin:0 auto-145px;}#frame_body{width:771px;margin:0 auto;}我用我的代码更新了它,我可以;t找出错误:您的元素没有高度,因此父元素的高度为0。好吧,这是有道理的。我使用的是粘性页脚,所以我希望页脚总是随着窗口向上移动。
$(document).ready(function () {
    var $stretch = $("#stretch");

    $stretch.animate({ height: $stretch.parent().height() }, 500 );
});