【问题标题】:jQuery: how to add height of childrens divs with class to parents divs with classjQuery:如何将带有类的子 div 的高度添加到带有类的父 div
【发布时间】:2016-06-01 16:21:44
【问题描述】:

我有一些带有class="parent" 的div,其中一部分包含带有class="image" 的div 或div。我想向这些包含图像 div 的父 div 添加第一个图像 div 的高度。每个 div 父级中的图像 div 具有不同的高度。

以下代码获取第一个图像 div 的高度,并将其添加到所有父 div。虽然我想将它的高度添加到每个父 div 真正的子图像 div。

var p = $('.image')
var maxheight = p.height()

$('.parent').filter(function() {
    return $(this).has('.image');
}).height(maxheight);

我该怎么办?

【问题讨论】:

  • 也许发布一些 HTML 会有所帮助。我不确定我能否按原样理解您的问题。
  • 我无法理解您要执行的操作。能否请您改写问题?

标签: jquery


【解决方案1】:

您需要遍历您的图像 div:

var p = $('.image');
p.each(function(index, item) {

});

现在,假设您的“父” div 实际上是图像的父容器,那么您可以这样做:

var p = $('.image');
p.each(function(index, item) {
    $(item).parent().height($(item).height());
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2022-09-23
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    相关资源
    最近更新 更多