jQuery width() 和 height() 方法

  1. width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。
  2. height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。
  1. <script>
  2. $(document).ready(function(){
  3. $("button").click(function(){
  4. var txt="";
  5. txt+="Width of div: " + $("#div1").width() + "</br>";
  6. txt+="Height of div: " + $("#div1").height();
  7. $("#div1").html(txt);
  8. });
  9. });
  10. </script>
  1. $(document).ready(function(){
  2. $("button").click(function(){
  3. $("#div1").width(320).height(320);
  4. });
  5. });
  6. 设置指定的 <div> 元素的宽度和高度:

jQuery innerWidth() 和 innerHeight() 方法

  1. innerWidth() 方法返回元素的宽度(包括内边距)。
  2. innerHeight() 方法返回元素的高度(包括内边距)。
  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() 方法

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

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

  1. 文档(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. });