课程表

jQuery 教程

jQuery 效果

jQuery HTML

jQuery 遍历

jQuery AJAX

jQuery 杂项和实例

jQuery 参考手册

工具箱
速查手册

jQuery 尺寸

当前位置:免费教程 » JS/JS库/框架 » jQuery

通过 jQuery,很容易处理元素和浏览器窗口的尺寸。

jQuery 尺寸 方法

jQuery 提供多个处理尺寸的重要方法:

  • width()
  • height()
  • innerWidth()
  • innerHeight()
  • outerWidth()
  • outerHeight()

jQuery width() 和 height() 方法

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

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

下面的例子返回指定的 <div> 元素的宽度和高度:

实例

  1. $("button").click(function(){
  2. var txt="";
  3. txt+="Width: " + $("#div1").width() + "</br>";
  4. txt+="Height: " + $("#div1").height();
  5. $("#div1").html(txt);
  6. });

亲自试一试

jQuery innerWidth() 和 innerHeight() 方法

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

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

下面的例子返回指定的 <div> 元素的 inner-width/height:

实例

  1. $("button").click(function(){
  2. var txt="";
  3. txt+="Inner width: " + $("#div1").innerWidth() + "</br>";
  4. txt+="Inner height: " + $("#div1").innerHeight();
  5. $("#div1").html(txt);
  6. });

亲自试一试

jQuery outerWidth() 和 outerHeight() 方法

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

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

下面的例子返回指定的 <div> 元素的 outer-width/height:

实例

  1. $("button").click(function(){
  2. var txt="";
  3. txt+="Outer width: " + $("#div1").outerWidth() + "</br>";
  4. txt+="Outer height: " + $("#div1").outerHeight();
  5. $("#div1").html(txt);
  6. });

亲自试一试

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

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

实例

  1. $("button").click(function(){
  2. var txt="";
  3. txt+="Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
  4. txt+="Outer height (+margin): " + $("#div1").outerHeight(true);
  5. $("#div1").html(txt);
  6. });

亲自试一试

jQuery - 更多的 width() 和 height()

下面的例子返回文档(HTML 文档)和窗口(浏览器视口)的宽度和高度:

实例

  1. $("button").click(function(){
  2. var txt="";
  3. txt+="Document width/height: " + $(document).width();
  4. txt+="x" + $(document).height() + "\n";
  5. txt+="Window width/height: " + $(window).width();
  6. txt+="x" + $(window).height();
  7. alert(txt);
  8. });

亲自试一试

下面的例子设置指定的 <div> 元素的宽度和高度:

实例

  1. $("button").click(function(){
  2. $("#div1").width(500).height(500);
  3. });

亲自试一试

jQuery CSS 参考手册

如需关于 jQuery Dimensions 的完整参考,请访问我们的 jQuery 尺寸参考手册。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号