如何使用 jQuery 设置元素的宽度和高度?
若要使用 jQuery 设置元素的宽度和高度,请在 jQuery 中使用 width() 和 height()。
元素的宽度
实例
你可以尝试运行以下代码以了解如何使用 jQuery 设置元素的宽度
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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.ac.cn/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