Javascript 通过比较两个高度改进设置div高度的脚本

Javascript 通过比较两个高度改进设置div高度的脚本,javascript,jquery,Javascript,Jquery,我写了一个小脚本,比较两个div的高度,并将最大div的高度设置为另一个div 我的问题是:如何改进这个脚本?。因为我现在正在重复获取2 div的兽皮的部分 $( document ).ready(function() { showHeight($( "#tab-content2" ).height()); showHeight($( "#tab-content1" ).height()); }); var bheight = 0; function showHeight(height)

我写了一个小脚本,比较两个div的高度,并将最大div的高度设置为另一个div

我的问题是:如何改进这个脚本?。因为我现在正在重复获取2 div的兽皮的部分

$( document ).ready(function() {
  showHeight($( "#tab-content2" ).height());
  showHeight($( "#tab-content1" ).height());
});
var bheight = 0;
function showHeight(height) {
  if( height > bheight){
    bheight = height;
    $("aside").height(bheight + 60);
  }
}

通过选择以选项卡内容开头的所有ID,然后使用

我猜是这样的:

$( document ).ready(function() {
  showHeight("[id^='tab']"); // pass the selector
});


function showHeight(elem) {
   var heights = $(elem).map(function (){
      return $(this).height();
   }).get(), // loops through the selectors and gets the height in array

   maxHeight = Math.max.apply(null, heights); // returns the max height value.
   $("aside").height(maxHeight + 60); // and set it here.
}

在选项卡中添加一些类

$(document).ready(function () {
    var bheight = 0; 
    $('.tabClass').each(function () {
        bheaight = Math.max(bheight, $(this).height());
    });
    $('aside').height(bheight + 60);
});
$(document).ready(function () {
    var bheight = 0; 
    $('.tabClass').each(function () {
        bheaight = Math.max(bheight, $(this).height());
    });
    $('aside').height(bheight + 60);
});