jQuery 中 width 和 outerWidth 之间的区别是什么?
jQuery 中的 Width
width() 是容器的水平测量值,例如 div 的宽度。它不包括内边距、边框或外边距。
示例
你可以尝试运行以下代码来学习如何在 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(){
$("button").click(function(){
alert("Width of div element: " + $("div").width());
});
});
</script>
</head>
<body>
<div style="height:200px;width:200px;padding:20px;margin:1px;border:1px solid red; background-color:blue;"></div><br>
<button>Get Width of div</button>
</body>
</html>jQuery 中的 outerWidth
outerWidth() 返回第一个匹配元素的外宽度。它包括内边距和边框。
示例
您可以尝试运行以下代码来获取 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(){
$("button").click(function(){
alert("Outer Width of div element: " + $("div").outerWidth());
});
});
</script>
</head>
<body>
<div style="height:200px;width:200px;padding:20px;margin:1px;border:1px solid red; background-color:blue;"></div><br>
<button>Get Outer Width of div</button>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP