如果我有两个div,其中父元素(我们称之为#parent)的高度是动态的,那么是否有办法使用jQuery使子元素div(我们称之为#child)保持相同的高度,并且随着父元素div响应式地改变高度而增加和减少?
Try...
var h = $("#parent").height();
$("#child").height(h);
或者
$("#child").height($("#parent").height()); //to save a line of code
$(document).ready()
中还是在自定义函数中。$("#child").height($("#parent").height()));
。哦,还有这个方法不能响应调整大小。 - j08691height()
的不同用法分开展示对于 jQuery 初学者来说更易理解。 - Kierchon你也可以通过CSS来实现,只需将子div的高度设置为100%即可
#parent
{
//css of parent div
}
#child{
height:100%;
}
display: inline-block 对我很有用。我在另一个 ng-repeat 中嵌套了一个 ng-repeat,而父元素始终为 0 高度。