標籤:boolean padding .com 擷取 bool pos source doc sel
曾經寫代碼中,每當須要擷取元素的實際“寬度”(這裡的寬度是指元素寬度加上其邊距)時,都須要用元素寬度加上margin值才行,今天發現一個叫outerWidth(options)的方法 ,非常好用。
完畢了原來須要用一段來操作做的工作。
這種方法不適用於window 和 document對象,能夠使用.width()取代。
以下是其簡介
outerWidth(options)
擷取第一個匹配元素外部寬度(預設包含補白和邊框)。
此方法對可見和隱藏元素均有效。
傳回值:Integer 一個整數值 不帶"px"
參數:
options(Boolean) : 預設值為false 設定為 true 時,計算邊距在內。
示範範例:
擷取第一段落外部寬度。
HTML 程式碼:
<div>
<div id="test" style="width:80px;margin:10px;"></div>
</div>
jQuery 代碼:
var w = $("#test").outerWidth(true);
$("#test").html(w);
結果:
<div>
<div id="test" style="width:80px;margin:10px;">100</div>
</div>
outerHeight(options)
擷取第一個匹配元素外部高度(預設包含補白和邊框)。
此方法對可見和隱藏元素均有效。
傳回值:Integer
參數:
options(Boolean) : 預設值為false 設定為 true 時,計算邊距在內。
示範範例:
擷取第一段落外部高度。
HTML 程式碼:
<div>
<div id="test" style="height:20px;margin:10px;"></div>
</div>
jQuery 代碼:
var h = $("#test").outerHeight(true);
$("#test").html(h);
結果:
<div>
<div id="test" style="height:20px;margin:10px;">40</div>
</div>
</div>
jquery outerHeight方法 outerWidth方法 擷取元素實際寬度高度