Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery将元素的高度设置为组中的最高值_Javascript_Jquery - Fatal编程技术网

Javascript jQuery将元素的高度设置为组中的最高值

Javascript jQuery将元素的高度设置为组中的最高值,javascript,jquery,Javascript,Jquery,我正在尝试使用jQuery从div中的前3个元素中找到最高的元素,然后将所有3个元素设置为相同的高度,然后检查下3个元素并设置它们。。等如果我的窗口宽度==X,也如果窗口宽度

我正在尝试使用jQuery从div中的前3个元素中找到最高的元素,然后将所有3个元素设置为相同的高度,然后检查下3个元素并设置它们。。等如果我的窗口宽度==X,也如果窗口宽度 这是我当前的代码,适用于所有元素,我只想按组(2和3)遍历元素,并根据结果和窗口大小设置该组的高度

// Find highest element and set all the elements to this height.
    $(document).ready(function () {

    // Set options
    var height = 0;
    var element_search = "#cat_product_list #cat_list";
    var element_set = "#cat_product_list  #cat_list";

    // Search through the elements set and see which is the highest.
    $(element_search).each(function () {
        if (height < $(this).height()) height = $(this).height();
        //debug_(height,1);
    });

    // Set the height for the element(s if more than one).
    $(element_set).each(function () {
        $(element_set).css("height", (height+40) + "px");
    });
});
//找到最高的元素并将所有元素设置为此高度。
$(文档).ready(函数(){
//设置选项
var高度=0;
var element_search=“#cat#U产品#U列表#cat#U列表”;
var元素_集=“#cat_产品_列表#cat_列表”;
//搜索元素集,看看哪个是最高的。
$(元素搜索)。每个(函数(){
如果(高度<$(此).height())高度=$(此).height();
//调试(高度1);
});
//设置元素的高度(如果有多个)。
$(元素集)。每个(函数(){
$(element_set).css(“高度”,(高度+40)+“px”);
});
});

非常感谢您的帮助:)

请尝试以下方法,将它们全部设置为最大高度:

$(document).ready(function() {
  var maxHeight = 0;
  $("#cat_product_list #cat_list").each(function() {
    if ($(this).outerHeight() > maxHeight) {
      maxHeight = $(this).outerHeight();
    }
  }).height(maxHeight);
});
更新22/09/16:您也可以使用CSS Flexbox在不使用任何Javascript的情况下实现相同的功能。将容器元素设置为具有
显示:flex
将自动将元素的高度设置为相同(在最高的元素之后)。

我现在已经对其进行了排序, 看看我的小提琴:

//找到最高的元素并将所有元素设置为此高度。
$(文档).ready(函数(){
//如果windows宽度小于此值,请执行以下操作
var windowWidth=$(window.width();
如果(窗宽<2000){
var i=0,
quotes=$(“目录产品列表”).children(),
组;
while((group=quotes.slice(i,i+=2)).length){
wrapAll组(“”);
}
}
//找到窗口的宽度
var windowwidth=$(window.width();
//调试(窗口宽度);
//设置选项
var高度=0;
var element_wrap=“.productWrap”;
var element_search=“.cat_list”;
//搜索元素集,看看哪个是最高的。
$(元素)。每个(函数(){
$(this).find(元素搜索).each(函数(){
如果(高度<$(此).height())高度=$(此).height();
});
//警报(“块高度:”+高度);
//设置元素包裹的高度。
$(this.css(“height”,(height)+“px”);
//未设置高度
高度=0;
});
});

只是为了添加另一个建议。下面是我编写的一个jQuery插件,它接受一个参数。你可以这样称呼它:

$('.elementsToMatch').matchDimensions("height");
可以匹配高度、宽度,如果未输入参数,则可以匹配两个尺寸

$(函数(){
$(.matchMyHeight”).matchDimensions(“高度”);
});
(函数($){
$.fn.matchDimensions=函数(维度){
var itemthematch=$(此),
最大高度=0,
最大宽度=0;
如果(itemthematch.length>0){
开关(尺寸){
案例“高度”:
css(“高度”、“自动”)。每个(函数(){
maxHeight=Math.max(maxHeight,$(this.height());
}).高度(最大高度);
打破
大小写“宽度”:
css(“宽度”、“自动”)。每个(函数(){
maxWidth=Math.max(maxWidth,$(this.width());
}).宽度(最大宽度);
打破
违约:
ItemTostMatch.each(函数(){
var thisItem=$(此项);
maxHeight=Math.max(maxHeight,thisItem.height());
maxWidth=Math.max(maxWidth,thisItem.width());
});
肚子痛
.css({
“宽度”:“自动”,
“高度”:“自动”
})
.高度(最大高度)
.宽度(最大宽度);
打破
}
}
返胃;
};
})(jQuery)
.matchMyHeight{background:#eee;float:左;width:30%;margin left:1%;padding:1%;}

第一组
第2组
第六组
第六组
第六组
第六组
var highHeight=“0”;
$(“.item”)。每个(函数(){
var thHeight=$(this).height();
如果(高度<高度){
highHeight=thHeight;
}
});
控制台日志(高高度)
$('.elementsToMatch').matchDimensions("height");
    var highHeight = "0";
    $(".item").each(function(){
        var thHeight = $(this).height();
        if(highHeight < thHeight ){
            highHeight = thHeight;
        }
    });

console.log(highHeight)