检测宽度:在jQuery中自动 [英] Detecting width: auto in jQuery

查看:191
本文介绍了检测宽度:在jQuery中自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery检索元素的宽度,如果我可以指示是否有明确的宽度(和高度)指定,喜欢它。

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>

这将提醒div的隐含宽度,屏幕。有没有什么办法,如果宽度丢失或设置为 width:auto ,它可以使用jQuery验证?

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?

也就是说,不是返回一个整数,而是返回 auto undefined 。或者,是否有等效的 isAuto 函数?

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?

推荐答案

我不相信这是可能的时刻。至少不在IE以外的任何其他浏览器。 IE实现 element.currentStyle ,它表示在CSS文件中写入的样式。另一方面,其余浏览器实现 window.getComputedStyle ,它返回这些样式的计算值

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.

唯一的办法就是从 document.styleSheets

The only way around it would be to parse CSS declarations from 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

这篇关于检测宽度:在jQuery中自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆