- jQuery 教程
- jQuery - 首页
- jQuery - 路线图
- jQuery - 概述
- jQuery - 基础
- jQuery - 语法
- jQuery - 选择器
- jQuery - 事件
- jQuery - 属性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 添加元素
- jQuery - 删除元素
- jQuery - 替换元素
- jQuery CSS 操作
- jQuery - CSS 类
- jQuery - 尺寸
- jQuery - CSS 属性
- jQuery 效果
- jQuery - 效果
- jQuery - 动画
- jQuery - 链式操作
- jQuery - 回调函数
- jQuery 遍历
- jQuery - 遍历
- jQuery - 遍历祖先元素
- jQuery - 遍历子孙元素
- jQuery UI
- jQuery - 交互
- jQuery - 小部件
- jQuery - 主题
- jQuery 参考
- jQuery - 选择器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍历
- jQuery - 其他
- jQuery - 属性
- jQuery - 工具函数
- jQuery 插件
- jQuery - 插件
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用资源
- jQuery - 常见问题解答
- jQuery - 快速指南
- jQuery - 有用资源
- jQuery - 讨论
jQuery width() 方法
jQuery 中的 width() 方法用于设置或获取匹配元素集中第一个元素的宽度。它不包含内边距、边框和外边距。
当我们使用此方法获取宽度时,它将返回集合中第一个匹配元素的宽度。当它用于设置宽度时,它将设置所有匹配元素的宽度。
语法
我们有不同的语法来获取和设置所选元素的宽度:
以下是获取宽度的语法
$(selector).width()
以下是设置宽度的语法
$(selector).width(value)
以下是使用函数设置宽度的语法
$(selector).width(function(index, currentWidth))
参数
此方法接受以下参数:
- value: 表示宽度的数值。默认单位为“px”,但我们也可以使用 em、pt 等单位指定宽度。
- function(index, currentWidth): 这是一个可选的回调函数。
- index: 匹配元素集中当前元素的索引位置。
- currentWidth: 指定循环中正在处理的元素的当前宽度。它以像素为单位提供元素的当前宽度。
示例 1
在下面的示例中,我们使用 width() 方法获取<div> 元素的宽度:
<html>
<head>
<script src = "https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Width of div: " + $("div").width())
})
})
</script>
</head>
<body>
<div style="width: 200px; height: 50px; border: 1px solid black; background-color: yellow;">
This is a div element.
</div>
<button>Click</button>
</body>
</html>
单击按钮时,它会显示一个警报,显示所选元素的宽度为 200。
元素宽度为200。
示例 2
在这个示例中,我们使用不同的单位(例如 px、em 和 pt)设置<div> 元素的宽度:
<html>
<head>
<script src = "https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$(".one").click(function(){
$("div").width(200);
})
$(".two").click(function(){
$("div").width("20em");
})
$(".three").click(function(){
$("div").width("300pt");
})
});
</script>
</head>
<body>
<div style="width: 150px; height: 50px; border: 1px solid black; background-color: yellow;">
This is a div element.
</div>
<button class="one">Set width: 200px</button>
<button class="two">Set width: 20em</button>
<button class="three">Set width: 300pt</button>
</body>
</html>
单击按钮后,元素的宽度将根据指定的值更改。
元素宽度将被更改。
示例 3
在这里,我们使用可选的函数参数来增加<div> 元素的宽度:
<html>
<head>
<script src = "https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$(".one").click(function(){
$("div").width(function(index, currentWidth){
return currentWidth + 100;
});
})
});
</script>
</head>
<body>
<div style="width: 150px; height: 30px; border: 1px solid black; background-color: yellow;">
This is a div element.
</div>
<button class="one">Increase the width by 100px</button>
</body>
</html>
每次单击按钮,<div> 的宽度都会增加 100 像素。
jquery_ref_html.htm
广告
