[Update]
最初的答案是在jQuery 1.3之前编写的,当时存在的函数本身不足以计算整个宽度.
现在,正如J-P正确声明的那样,jQuery有函数outerWidth和outerHeight,缺省情况下包括border
和padding
,如果函数的第一个参数是true
,还包括margin
[Original answer]个
The width
method no longer requires the dimensions
plugin, because it has been added to the jQuery Core
您需要做的是获取特定div的padding、margin和border width值,并将它们添加到width
方法的结果中
比如:
var theDiv = $("#theDiv");
var totalWidth = theDiv.width();
totalWidth += parseInt(theDiv.css("padding-left"), 10) + parseInt(theDiv.css("padding-right"), 10); //Total Padding Width
totalWidth += parseInt(theDiv.css("margin-left"), 10) + parseInt(theDiv.css("margin-right"), 10); //Total Margin Width
totalWidth += parseInt(theDiv.css("borderLeftWidth"), 10) + parseInt(theDiv.css("borderRightWidth"), 10); //Total Border Width
Split into multiple lines to make it more readable
这样,即使更改了css中的填充或边距值,也总能得到正确的计算值