jquery尺寸:宽度与高度

时间:2023-12-10 17:38:38

  width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。

  height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。

  innerWidth() 方法返回元素的宽度(包括内边距)。

  innerHeight() 方法返回元素的高度(包括内边距)。  

  outerWidth() 方法返回元素的宽度(包括内边距和边框)。

  outerHeight() 方法返回元素的高度(包括内边距和边框)。

  outerWidth(true) 方法返回元素的宽度(包括内边距、边框和外边距)。

  outerHeight(true) 方法返回元素的高度(包括内边距、边框和外边距)。

  $(document).width()与$(window).width()返回文档(HTML 文档)和窗口(浏览器视口)的宽度和高度。

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="css/all.css" />
<style type="text/css">
div { margin: 10px; padding: 10px; width: 300px; height: 200px; border: 10px solid #ccc; }
</style>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/all.js"></script>
</head> <body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
<script type="text/javascript">
var s1 ='width()是' + $('div').width() +'px,因为width()不包含margin,padding,border';
var s2 ='innerWidth()是' + $('div').innerWidth() +'px,因为包含了padding,左右各10px';
var s3 ='outerWidth()是' + $('div').outerWidth() +'px,因为包含了padding(左右名10px)与border(左右名10px)';
var s4 ='outerWidth(true)是' + $('div').outerWidth(true) +'px,包含了padding,margin,border,左右名10px';
var s5 ='document文档的width()是' + $(document).width() +'px';
var s6 ='window窗口的width()是' + $(window).width() +'px';
alert(s1);
alert(s2);
alert(s3);
alert(s4);
alert(s5);
alert(s6);
alert('当没有垂直滚动条的时候,$(document).width()与$(window).width()在我的电脑上的宽度是1366,而在有垂直滚动条的时候,在我的电脑上的宽度是1349。并且当调整浏览器窗口的大小的时候,这两个值也会相应的发生变化。');
</script>