如何使用 jQuery 設定元素的寬度和高度?
若要使用 jQuery 設定元素的寬度和高度,請在 jQuery 中使用 width() 和 height()。
元素的寬度
例項
你可以嘗試執行以下程式碼以瞭解如何使用 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(){
$("#button1").click(function(){
$("div").width(300);
});
$("#button2").click(function(){
$("div").width(400);
});
});
</script>
</head>
<body>
<div style="height:150px;width:200px;border:2px solid gray;background-color:lightblue;"></div><br>
<button id="button1">Width of div: 300</button>
<button id="button2">Width of div: 400</button><br>
</body>
</html>元素的高度
例項
你可以嘗試執行以下程式碼以瞭解如何使用 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(){
$("#button1").click(function(){
$("div").height(250);
});
$("#button2").click(function(){
$("div").height(350);
});
});
</script>
</head>
<body>
<div style="height:150px;width:400px;border:2px solid gray;background-color:lightblue;"></div><br>
<button id="button1">Height of div: 250</button>
<button id="button2">Height of div: 350</button><br>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP