文章目录
jQuery - 尺寸
使用 jQuery,可以轻松处理元素和浏览器窗口的尺寸。
jQuery 尺寸方法
jQuery 有几种处理尺寸的重要方法:
- width()
- height()
- innerWidth()
- innerHeight()
- outerWidth()
- outerHeight()
jQuery width() 和 height() 方法
width() 方法设置或返回元素的宽度(不包括填充、边框和边距)。
height() 方法设置或返回元素的高度(不包括填充、边框和边距)。
以下示例返回指定 <div>
元素的宽度和高度:
示例
$("button").click(function(){
var txt = "";
txt += "Width: " + $("#div1").width() + "</br>";
txt += "Height: " + $("#div1").height();
$("#div1").html(txt);
});
jQuery innerWidth() 和 innerHeight() 方法
innerWidth() 方法返回元素的宽度(包括填充)。
innerHeight() 方法返回元素的高度(包括填充)。
以下示例返回指定 <div>
元素的内部宽度/高度:
示例
$("button").click(function(){
var txt = "";
txt += "内部宽度: " + $("#div1").innerWidth() + "</br>";
txt += "内部高度: " + $("#div1").innerHeight();
$("#div1").html(txt);
});
jQuery outerWidth() 和 outerHeight() 方法
outerWidth() 方法返回元素的宽度(包括填充和边框)。
outerHeight() 方法返回元素的高度(包括填充和边框)。
以下示例返回指定 <div>
元素的外部宽度/高度:
示例
$("button").click(function(){
var txt = "";
txt += "外部宽度: " + $("#div1").outerWidth() + "</br>";
txt += "外部高度: " + $("#div1").outerHeight();
$("#div1").html(txt);
});
outerWidth(true) 方法返回元素的宽度(包括填充、边框和边距)。
outerHeight(true) 方法返回元素的高度(包括填充、边框和边距)。
示例
$("button").click(function(){
var txt = "";
txt += "外部宽度 (+margin): " + $("#div1").outerWidth(true) + "</br>";
txt += "外部高度 (+margin): " + $("#div1").outerHeight(true);
$("#div1").html(txt);
});
jQuery 更多 width() 和 height()
以下示例返回文档(HTML 文档)和窗口(浏览器视口)的宽度和高度:
示例
$("button").click(function(){
var txt = "";
txt += "文档宽度/高度: " + $(document).width();
txt += "x" + $(document).height() + "\n";
txt += "窗口宽度/高度: " + $(window).width();
txt += "x" + $(window).height();
alert(txt);
});
以下示例设置指定 <div>
元素的宽度和高度:
示例
$("button").click(function(){
$("#div1").width(500).height(500);
});
总结
本文介绍了jQuery - 尺寸函数使用,如有问题欢迎私信和评论