$(window).on("resize", function () {
var totalHeight = 0
var $p = $("#cwt-help").children();
$p.each(function () {
totalHeight += $("#cwt-help").outerHeight();
});
$("#cwt-help").css({ "height": totalHeight })
});
我试图在窗口调整大小后调整父 div 的大小以匹配其子项的高度。我遇到的问题是这段代码在调整大小之前得到了高度。我怎样才能得到最终的高度?谢谢!
最佳答案
代码在resize
事件发生时执行...
并且您希望它在事件发生后执行。
那么 setTimeout()
呢? ?
我想事件发生后 100 毫秒就足够了。
$(window).on("resize", function () {
setTimeout(function(){
var totalHeight = 0
var $p = $("#cwt-help").children();
$p.each(function () {
totalHeight += $("#cwt-help").outerHeight();
});
$("#cwt-help").css({ "height": totalHeight });
},100);
});
关于javascript - 调整窗口大小后,将父级 (div) 高度更改为其子级 (p) 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44979992/