本文透過實例講解 jQuery 中 outerHeight() 方法的用法,分享給大家參考。具體分析如下:
outerHeight() 用來取得第一個匹配元素的外部高度。預設情況下,外部高度是 height、padding 和 border 三者之和。如果把參數設為 true,則連 margin 也會計入外部高度。此方法對可見和隱藏元素都有效,建議與 outerWidth() 一起學習。
語法結構:
程式碼如下:$(selector).outerHeight(options)
參數列表:
options:定義是否把外距(margin)計算在內:
一、false,邊距不計算在內,為預設值。
二、true,邊距計算在內。
實例程式碼:
程式碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="https://www.cnblogs.com/" />
<title>outerHeight() 函數-博客園</title>
<style type="text/css">
div{
background-color:green;
height:100px;
width:200px;
padding:10px;
margin:10px;
border:5px solid red;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("div").text($("div").outerHeight(true))
})
})
</script>
</head>
<body>
<div>此處顯示 outerHeight 數值</div>
<button>點擊查看 div 的 outerHeight</button>
</body>
</html>
點擊按鈕即可顯示 div 元素的外部高度。以本例而言,傳入 true 後,outerHeight() 會把 height、padding、border 及 margin 全部計入。
希望本文所述對大家的 jQuery 程式設計有所幫助。