如何使用 jQuery 獲取 div 元素的寬度?
要獲取 div 元素的寬度, 請在 jQuery 中使用 width() 方法。
範例
你可以嘗試執行以下程式碼來使用 jQuery 獲取 div 元素的寬度
<!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:450px;padding:20px;margin:1px;border:1px solid red;background-color:gray;"></div><br>
<button>Get Width of div</button>
</body>
</html>
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP