在 jQuery 中 height 和 outerHeight 的區別是什麼?


jQuery 中的 height

height() 是容器的縱向度量,例如 div 的高度。它不包括內邊距、邊框和外邊距。

要獲得 jQuery 中元素的高度,請在 jQuery 中使用 height() 方法。

示例

你可以嘗試執行以下程式碼來獲取高度

現場演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert("Height of div element: " + $("div").height());
    });
});
</script>
</head>
<body>

<div style="height:100px;width:350px;padding:10px;margin:10px;border:1px solid red; background-color:blue;"></div><br>
<button>Get Height of div</button>

</body>
</html>

jQuery 中的 outerHeight()

outerHeight() 返回首個匹配元素的外高度。它包括內邊距和邊框。

示例

你可以嘗試執行以下程式碼來獲取 jQuery 中的外高度

現場演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert("Outer Height of div element: " + $("div").outerHeight());
    });
});
</script>
</head>
<body>

<div style="height:200px;width:450px;padding:10px;margin:10px;border:1px solid red; background-color:blue;"></div><br>
<button>Get Outer Height of div</button>

</body>
</html>

更新於: 17-12-2019

1k+ 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.