jQuery outerWidth() 方法用于获取元素的宽度,包括元素的 padding 和 border,但不包括 margin。
// 获取元素的宽度(包括 padding 和 border) $(selector).outerWidth();
outerWidth() 方法返回一个数值,表示元素的宽度(以像素为单位)。如果未传递参数,则返回当前计算样式中的宽度值。如果传递了布尔值 true ,则返回元素在内容、padding 和 border 之外所占用的宽度。
// 获取元素在内容、padding 和 border 之外所占用的宽度 $(selector).outerWidth(true);
outerWidth() 方法也可以传递一个数字作为参数来设置元素的新宽度。此时该方法将返回 undefined :
// 设置元素新的宽度 $(selector).outerWidth(500); // 返回 undefined
需要注意的是 outerWidth() 方法不能用于 window 对象或 document 对象。如果要获取浏览器可视区域大小,请使用 $(window).width() 或 $(document).width() 来代替。
jQuery HTML/CSS 方法
返回 <div> 元素的外部高度:
outerHeight() 方法返回第一个匹配元素的外部高度。
如下面的图像所示,该方法包含 padding 和 border。
提示:如需包含 margin,请使用 outerHeight(true)。
相关方法:
参数 | 描述 |
---|---|
includeMargin | 可选。布尔值,规定是否包含 margin。
|
包含 margin
规定是否包含 margin。
通过相关方法显示尺寸
如何使用 width()、height()、innerHeight()、innerWidth()、outerWidth() 和 outerHeight()。
jQuery HTML/CSS 方法
jQuery 数据参考手册实例从元素中删除之前添加的数据:$(#btn2).click(function(){$(div).removeData(greeting);alert(Greeting ...
jQuery event.pageY 属性jQuery 事件方法实例 返回鼠标指针的位置:$(document).mousemove(function(event){$(span).text(X:+ ev...
jQuery hover() 方法jQuery 事件方法实例 当鼠标指针悬停在上面时,改变 p 元素的背景颜色:$(p).hover(function(){ $(p).css(ba...
jQuery append() 方法jQuery HTML/CSS 方法实例 在所有 p 元素结尾插入内容:$(button).click(function(){ $(p).append(bAppende...
jQuery remove() 方法jQuery HTML/CSS 方法实例 移除所有的 p 元素:$(button).click(function(){ $(p).remove(); }); 定义和用...