jQuery outerHeight() 方法


jQuery 中使用 outerHeight() 方法可返回一個元素的高度。此方法包含內邊距和邊框。

語法

語法如下−

$(selector).outerHeight(margin)

上述情況下,margin 是一個布林值,用於指定是否包含外邊距。TRUE 表示包含外邊距。

示例

現在,我們來看一個例子,實現 jQuery outerHeight() 方法

 線上演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         document.getElementById("demo").innerHTML = "<br>Outer Height of DIV = " + $("div").outerHeight()
      });
   });
</script>
</head>
<body>
<h2>Rectangular Box</h2>
<div style="height:200px;width:500px;padding:30px;margin:1px;background-color:orange;"></div><br>
<button>Outer height of div</button>
<p id="demo"></p>
</body>
</html>

輸出

這將產生以下輸出−

單擊“div 的外部高度”按鈕以獲取外部高度−

更新於: 2019-12-30

170 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始吧
廣告