jquery outerHeight方法 outerWidth方法 獲取元素實際寬度高度


曾經寫代碼中,每當須要獲取元素的實際“寬度”(這里的寬度是指元素寬度加上其邊距)時,都須要用元素寬度加上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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM