菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP How To W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

jQuery - Dimensions (尺寸)


使用 jQuery,可以轻松处理元素和浏览器窗口的尺寸。


jQuery Dimension (尺寸) 方法

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

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

jQuery Dimensions (尺寸)

jQuery Dimensions


jQuery width() 和 height() 方法

width() 方法用于设置或返回元素的宽度(不包括 padding、border 和 margin)。

height() 方法用于设置或返回元素的高度(不包括 padding、border 和 margin)。

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

示例

$("button").click(function(){
  var txt = "";
  txt += "Width: " + $("#div1").width() + "</br>";
  txt += "Height: " + $("#div1").height();
  $("#div1").html(txt);
});
自己动手试一试 »


jQuery innerWidth() 和 innerHeight() 方法

innerWidth() 方法返回元素的宽度(包括 padding)。

innerHeight() 方法返回元素的高度(包括 padding)。

以下示例返回指定 <div> 元素的内部宽度/高度:

示例

$("button").click(function(){
  var txt = "";
  txt += "Inner width: " + $("#div1").innerWidth() + "</br>";
  txt += "Inner height: " + $("#div1").innerHeight();
  $("#div1").html(txt);
});
自己动手试一试 »

jQuery outerWidth() 和 outerHeight() 方法

outerWidth() 方法返回元素的宽度(包括 padding 和 border)。

outerHeight() 方法返回元素的高度(包括 padding 和 border)。

以下示例返回指定 <div> 元素的外部宽度/高度:

示例

$("button").click(function(){
  var txt = "";
  txt += "Outer width: " + $("#div1").outerWidth() + "</br>";
  txt += "Outer height: " + $("#div1").outerHeight();
  $("#div1").html(txt);
});
自己动手试一试 »

outerWidth(true) 方法返回元素的宽度(包括 padding、border 和 margin)。

outerHeight(true) 方法返回元素的高度(包括 padding、border 和 margin)。

示例

$("button").click(function(){
  var txt = "";
  txt += "Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
  txt += "Outer height (+margin): " + $("#div1").outerHeight(true);
  $("#div1").html(txt);
});
自己动手试一试 »

jQuery 更多 width() 和 height() 应用

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

示例

$("button").click(function(){
  var txt = "";
  txt += "Document width/height: " + $(document).width();
  txt += "x" + $(document).height() + "\n";
  txt += "Window width/height: " + $(window).width();
  txt += "x" + $(window).height();
  alert(txt);
});
自己动手试一试 »

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

示例

$("button").click(function(){
  $("#div1").width(500).height(500);
});
自己动手试一试 »

jQuery 练习

通过练习来测试自己

练习

使用 jQuery 方法将 <div> 的高度和宽度设置为 500 像素。

$("div").().();

开始练习


jQuery CSS 参考

有关所有 jQuery CSS 方法的完整概述,请访问我们的 jQuery HTML/CSS 参考


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持