2011-08-31 64 views
0

如何確定元素使用的是css height還是css max-height如何確定元素正在使用高度還是最大高度?

例如,我有這兩個要素,

<div style="height:100px">fixed</div> 
<div style="max-height:100px">max-height</div> 

我想改變自己的height/ max-height100%一個按鈕被點擊時。

$('div').height('100%').height(); 

這隻會改變第一個元素而不是第二個元素。

我想用if條件檢查:

if(use height) $('div').height('100%').height(); 
else $('div').css({maxHeight:'100%'}).height(); 

但我不知道如何檢查...任何想法?或者你可能比我的想法更好?

回答

1

您可以使用類似下面的代碼一次性設置它們兩個。

$('#div').css({height: "100%", maxHeight:"100%"}); 

希望這是有用的!

+0

謝謝!這已經解決了我的問題!謝謝! – laukok

+0

我只是點擊它作爲我的答案。謝謝 :) – laukok

0

我相信你可以測試.css('maxHeight')。

// Returns 'none' 
$('img').css('maxHeight') 

// Returns '32px' 
$('img').css('height') 
0

使用$('div').css('max-height')會給你設定值或undefined'none'視情況而定。

同樣適用於'height'或任何其他css屬性。