Javascript HTML Div等高
我已经编写了这段javascript代码,但我想我是在重复我自己的想法,因此,有什么建议可以让这段代码更好、更优化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() { $(
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');
你的问题是什么,具体点。这可能会有帮助,因为代码中没有问题。。只是需要一个更优化的版本当寻找一个代码审查,请使用该网站。