我假设你真的有一个img
元素实例(你已经显示的可以采取任何方式,元素或字符串,但你没有引用它们,所以...)的数组。
你也可以使用jQuery的$.each
:
var maxHeight = -1;
$.each(theArray, function() {
if (maxHeight < this.height) {
maxHeight = this.height;
}
});
或者,如果你已经有了阵列包裹在一个jQuery对象:
var theArray = $('img'); // A jQuery object wrapped around an array of all `img`s
...那么你可以使用each
(这是稍微不同于$.each
):
var maxHeight = -1;
theArray.each(function() {
if (maxHeight < this.height) {
maxHeight = this.height;
}
});
或者只是使用一个无聊的老式循环:
var index, maxHeight;
maxHeight = -1;
for (index = 0; index < theArray.length; ++index) {
if (maxHeight < theArray[index].height) {
maxHeight < theArray[index].height;
}
}
在上述所有的,我已经使用了HTMLImageElement
的height
property由DOM指定。或者,您可能更喜欢jQuery的height()
函数(如果是这样,不要忘记将元素包装在jQuery对象 —,例如$(this).height()
),但通常height
(属性)是您需要的所有元素和img
元素。
该代码不太正确,但其背后的想法正是w我正在寻找的帽子。谢谢。 – 2011-04-29 04:54:06