我假設你真的有一個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