2015-03-31 62 views
0

我寫了一個小腳本,比較了2個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); 
    } 
} 

回答

0

通過選擇與tab-content開始,然後遍歷他們.each

$(document).ready(function() { 
    $("div[id^='tab-content']").each(function(){ 
     showHeight($(this).height()); 
    }); 
}); 
0

所有的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. 
} 
0

添加一些類到您的選項卡。

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