特定のクラスのアイテムごとに、それぞれをループして最も高いものを探し、残りを同じ高さに設定する関数を構築しています。
問題は、単一のクラス名しか使用していないことと、要素の型 (つまり、LI と DIV) に応じてそれぞれを一意に処理する必要があるさまざまな要素の型をキャッチしていることです。
var makeModulesSameHeight = function() {
var maxHeight,
$arrAllTargets = $(".makeEqualHeight");
maxHeight = getMaxHeight($arrAllTargets);
$arrAllTargets.css("height",maxHeight);
console.log("max height for every friggin' thing is " + maxHeight);
};
var getMaxHeight = function($arrModules){
var myHeight,maxHeight = 0;
$arrModules.each(function(){
myHeight = $(this).outerHeight();
maxHeight = (myHeight > maxHeight) ? myHeight : maxHeight;
});
return maxHeight;
};
makeModulesSameHeight();
フィドル - > http://jsfiddle.net/scott_in_ct/bpKxQ/
要素の種類に応じてこれを機能させる良い方法はありますか?
私は次のようなことを考えていました:
// initialize empty list to contain unique tag names
// select jquery master list of all items with same class
// for each item in jquery master list
// - if this tagName is not in list above, add it
// then
// for each item in tagName list
// - create new array specific to that tagName
(not sure how to do this, actually. I think I'm getting confused
with PHP's associative array.)
I guess I can always just remember what ordinal number
goes with which item.
// for each item in jquery master list
// - move this item into its appropriate tag array
// for each tag array
// - set each item in array to max height using previous implementation
これは、複数のループなどでこれを行うには厄介な方法のようです。おそらくメモ化または何かクールなものを使用して、誰かがより効率的なアプローチを持っているかどうかを確認しています。:^)
また
// for each item in master array
// -- look to a common parent and set height according to its descendents
// (somehow)
// profit!