Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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 HTML Div等高_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript HTML Div等高

Javascript HTML Div等高,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我已经编写了这段javascript代码,但我想我是在重复我自己的想法,因此,有什么建议可以让这段代码更好、更优化 var countHeight = []; $('.box').each(function() { countHeight.push( $(this).outerHeight() ); }); var maxValueInArray = Math.max.apply(Math, countHeight); $('.box').each(function() { $(

我已经编写了这段javascript代码,但我想我是在重复我自己的想法,因此,有什么建议可以让这段代码更好、更优化

var countHeight = [];
$('.box').each(function() {
    countHeight.push( $(this).outerHeight() );
});
var maxValueInArray = Math.max.apply(Math, countHeight);

$('.box').each(function() {

  $(this).css('height', maxValueInArray+'px');

});

这是我能想到的一组优化

$(function(){

var maxHeight = 0;
$('.box').each(function() {
    var h = $(this).outerHeight();
    if(h>maxHeight) maxHeight=h;
});

$('.box').each(function() {
  $(this).css('height', maxHeight+'px');
});

});

这里是略短的版本。无需使用第二个循环为每个.box元素添加高度

var countHeight = [];
$('.box').each(function() {
    countHeight.push( $(this).outerHeight() );
}).css('height', Math.max.apply(Math, countHeight)+'px');

你的问题是什么,具体点。这可能会有帮助,因为代码中没有问题。。只是需要一个更优化的版本当寻找一个代码审查,请使用该网站。