jquery最大高度和最大宽度返回错误的值

jquery max-height and max-width returns wrong values

本文关键字:错误 返回 高度 jquery      更新时间:2023-09-26

im在最大宽度和最大高度方面存在问题。假设我有以下css规则:

img.item { max-width: 325px; max-height: 390px; }

以及我的文档中的以下img:

<img class="item" src="http://us.dorothyperkins.com/wcsstore/DorothyPerkinsUS/images/catalog/80000095_normal.jpg">

原始图像宽度x高度为:342 x 464

如果我尝试使用jquery获取最大宽度和最大高度的值,我会弄错它们。

$('img.item').css('max-width');
// returns 357px
$('img.item').css('max-height');
// returns 429px

有什么想法吗?或者我怎样才能得到正确的尺寸?

如果我使用$('img.item').width() and $('img.item').height(),我会得到计算值,这对我来说不好。

谢谢。

我已经检查了您的代码并添加了最新的jquery,它运行良好,结果正确。请在这里检查我的代码。

alert($('img.item').css('max-width'));
alert($('img.item').css('max-height'));
img.item { max-width: 325px; max-height: 390px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="item" src="http://us.dorothyperkins.com/wcsstore/DorothyPerkinsUS/images/catalog/80000095_normal.jpg">

可能还有其他原因,您的图像max-heightmax-width可能被其他class

覆盖

奇怪的结果。我在上面创作了小提琴http://jsfiddle.net/JGwqY/并且最大宽度和最大高度值是正确的。