jQuery 中 width 與 innerWidth 的區別是什麼?
jQuery 中的寬度
寬度是容器的水平測量值,例如 div 的寬度。不包括填充、邊框或邊距。
示例
您可以嘗試執行以下程式碼來學習如何在 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("Width of div element: " + $("div").width());
});
});
</script>
</head>
<body>
<div style="height:200px;width:400px;padding:20px;margin:1px;border:1px solid red; background-color:gray;"></div><br>
<button>Get Width of div</button>
</body>
</html>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:400px;padding:20px;margin:1px;border:1px solid red; background-color:gray;"></div><br>
<button>Get Inner Width of div</button>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP