我正在构建一个函数,对于具有给定类的每个项目,循环遍历它们中的每一个,寻找最高的,然后将其余的设置为相同的高度。
问题是我只使用了一个类名,并且我正在捕获不同的元素类型,每个元素类型都应该根据它们的元素类型进行唯一处理(即 LIs 与 DIVs)
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!