我有一個div:.WIDTH()返回4個像素太多
<div class="frame" id="layer1"> ... </div>
與造型:
.frame {
position: absolute;
width: 380px;
height: 280px;
overflow: hidden;
padding: 0;
margin: 0;
border: 0;
}
#layer1 { background-color: red; color: #fff; }
但這些都是我的結果:
$(".frame").filter(":first").width(); // 384
$(".frame").filter(":first").outerWidth(); // 384
$(".frame").filter(":first").innerWidth(); // 384
我明白他們都是相同的,因爲我的填充和邊距是0,但這個默認4px從哪裏來?
FireBug/Chrome元素檢查器報告的寬度是多少? –
試試這個:['box-sizing:border-box'](http://www.css3.info/preview/box-sizing/)以確保它正好是380px,即使是填充也是如此。 –
在Chrome,Firefox和IE中我得到了380個:http://jsbin.com/isehud另外請注意,不需要執行'.filter(「:first」)',jQuery * getters *總是隻能從集合中的第一個元素。 (如果你確實需要這樣做,'.first()'而不是'.filter(「:first」)'可能是更好的方法,或者'.eq(0)'。 –