开发者

jQuery how to compare two CSS values

Hello I want to do the following:

if ($(this).height() == $(this).css('max-height')) {}

Proble开发者_Python百科m is that outputs:

console.log( $(this).height() + '  '  + $(this).css('max-height'))

as:

140  140px

Any ideas on how to normalize this without hacking or adding crazy amounts of code?


http://api.jquery.com/height/

The difference between .css('height') and .height() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .height() method is recommended when an element's height needs to be used in a mathematical calculation.

So, this should work:

if ($(this).css('height') == $(this).css('max-height')) {}


Easy as hell:

if(($(this).height() + "px") == etc…


if (($(this).height() + 'px') == $(this).css('max-height')) {}

Note that .height() gets the computed height, not a CSS value.


This worked better for me:

if ($(this).height() == parseInt($(this).css('max-height'))) {}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜