如何确定元素是使用高度还是最大高度?

时间:2011-08-31 21:18:56

标签: jquery height css

如何确定元素正在使用css heightcss max-height

例如,我有这两个元素,

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

我想在点击按钮时将height/ max-height更改为 100%

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

这只会改变第一个元素而不是第二个元素。

我正在考虑使用if条件来检查:

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

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

3 个答案:

答案 0 :(得分:1)

您可以使用类似下面的代码来同时设置它们。

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

希望这很有用!!

答案 1 :(得分:0)

我相信你可以测试.css('maxHeight')。

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

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

答案 2 :(得分:0)

使用$('div').css('max-height')会根据具体情况为您提供设定值或undefined'none'

同样适用于'height'或任何其他css属性。

相关问题