javascript - 调整窗口大小后,将父级 (div) 高度更改为其子级 (p) 的大小

标签 javascript jquery html css

$(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/

相关文章:

html - SVG 文件的剪切路径

javascript - 将 2 个 api 响应的结果合并到一个 JavaScript 数组中

javascript - IE中的鼠标位置

javascript - 如何使用 onclick 函数调用 div 标签元素和另一个 div 标签元素

javascript - 单击按钮停止启动功能

html - 检测悬停在 div 内

javascript - framework7 防止缓存页面数据

javascript - 用 "\\"写 JSON

jquery - 编写一个漂亮的 jQuery 数组

javascript - AngularJS ng-click 转到另一个页面(使用 Ionic 框架)