jQuery 內的 innerWidth 與 outerWidth 有什麼區別?


jQuery 中的 innerWidth

innerWidth() 返回第一個匹配元素的內部寬度。它包括填充,但不包括邊框和邊距。

示例

可以嘗試執行以下程式碼以在 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("Inner Width of div element: " + $("div").innerWidth());
    });
});
</script>
</head>
<body>

<div style="height:200px;width:500px;padding:20px;margin:1px;border:1px solid red; background-color:gray;"></div><br>
<button>Get Inner Width of div</button>

</body>
</html>

jQuery 中的 outerWidth

outerWith() 返回第一個匹配元素的外部寬度。它包括填充和邊框。

示例

可以嘗試執行以下程式碼以在 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 Width of div element: " + $("div").outerWidth());
    });
});
</script>
</head>
<body>

<div style="height:200px;width:500px;padding:20px;margin:1px;border:1px solid red; background-color:gray;"></div><br>
<button>Get Outer Width of div</button>

</body>
</html>

更新時間: 10-12-2019

219 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.