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

outerWidth() 返回第一個匹配元素的外部寬度。它包括內邊距和邊框。

示例

你可以嘗試執行以下程式碼來獲取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>

更新日期:2019年12月10日

219次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.