检测宽度:jQuery中的auto [英] Detecting width: auto in jQuery
问题描述
我正在使用jQuery检索元素的宽度,如果我可以指出是否指定了一个明确的宽度(和高度),则会更喜欢它。
< div id =test>< / div>
< script type =text / javascript>
$(function(){alert($('#test')。css('width'));});
< / script>
这将提醒div的隐含宽度,就客户端屏幕。有没有办法,如果宽度丢失或设置为 width:auto
可以使用jQuery验证?
也就是说,代替上述示例返回一个整数,它将返回
auto
或 undefined
。或者,还有一个等价于 isAuto
函数的代码?我不相信这是可能的。至少不是在任何其他浏览器比IE。 IE实现 element.currentStyle
,它们表示在CSS文件中编写的样式。另一方面,其余浏览器实现 window.getComputedStyle
,返回这些样式的计算值。这就是你在那里收到的,一个数字值而不是auto。
唯一的办法是从 document.styleSheets
。
参考文献:
- http://msdn.microsoft.com/en-us/library/ms535231(VS.85).aspx
- https://developer.mozilla.org/en/DOM:window.getComputedStyle
- https://developer.mozilla。 org / en / CSS / computed_value
I'm retrieving the width of elements using jQuery and would prefer it if I could have an indication of whether there was an explicit width (and height) specified.
<div id="test"></div>
<script type="text/javascript">
$(function() { alert($('#test').css('width')); });
</script>
This will alert the implicit width of the div in terms of how many pixels it takes up on the client's screen. Is there any way that if the width is either missing or set as width: auto
that it can be verified using jQuery?
That is, instead of the above example returning an integer, it would return either auto
or undefined
. Or, alternatively, is there an equivalent of a isAuto
function?
I don't believe it's possible for the moment. At least not in any other browser than IE. IE implements element.currentStyle
which represents styles at they were written in the CSS file. On the other hand, the rest of the browsers implement window.getComputedStyle
which returns the computed values of those styles. That's what you receive there, a numeric value instead of auto.
The only way around it would be to parse CSS declarations from document.styleSheets
.
References:
- http://msdn.microsoft.com/en-us/library/ms535231(VS.85).aspx
- https://developer.mozilla.org/en/DOM:window.getComputedStyle
- https://developer.mozilla.org/en/CSS/computed_value
这篇关于检测宽度:jQuery中的auto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!