Javascript jQuery如何获得innerWidth但没有填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7567915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:46:27  来源:igfitidea点击:

jQuery how to get innerWidth but without the padding?

javascriptjqueryhtmlcss

提问by Chris

From the docs, innerWidth does almost what I need:

从文档中,innerWidth 几乎可以满足我的需求:

"Gets the inner width (excludes the border and includes the padding) for the first matched element."

“获取第一个匹配元素的内部宽度(不包括边框并包括填充)。”

I need to know the width excludingthe padding. I.e. the usable space inside the element.

我需要知道不包括填充的宽度。即元素内部的可用空间。

Does jquery provide anything like this - done a bit of googling and can't find any solutions.

jquery 是否提供这样的东西 - 做了一些谷歌搜索,但找不到任何解决方案。

I thought about getting the padding-leftand padding-rightvalues to subtract from inner width- But given these could be percentages, pixels or em I'm not sure if this would be reliable.

我想过从内部宽度中减去padding-leftpadding-right值 - 但鉴于这些可能是百分比、像素或 em,我不确定这是否可靠。

Any suggestions?

有什么建议?

回答by Ben

You're probably just looking for the width() function..

您可能只是在寻找 width() 函数..

See the docs, it excludes the padding: http://api.jquery.com/width/

查看文档,它不包括填充:http: //api.jquery.com/width/

(Just as intended in modern browser's representation of the css width property)

(正如现代浏览器对 css width 属性的表示一样)

Edit:It's now 2012 and jQuery 1.8 is just coming out. While this is still relevant, you may also want to read the following article from the jQuery blogregarding box-sizingconsiderations in version 1.8

编辑:现在是 2012 年,jQuery 1.8 刚刚发布。虽然这仍然相关,但您可能还想阅读jQuery 博客中有关box-sizing1.8 版注意事项的以下文章

回答by daryl

$('#element').width();

It's as simple as that!

就这么简单!