目录
1. jQuery
1.1. $.width()、$.height()
1.2. $.innerWidth()、$.innerHeight()
1.3. $.outerWidth()、$.outerHeight()
1.4. $.outerWidth(true)、$.outerHeight(true)
2. 原生操作
2.1. offsetWidth、offsetHeight
2.2. clientWidth、clientHeight
2.3. scrollWidth、scrollHeight
3. 几道笔试题
1. jQuery
1.1. $.width()、$.height()
Get the current computed width for the first element in the set of matched elements.
1.2. $.innerWidth()、$.innerHeight()
Get the current computed inner width for the first element in the set of matched elements, including padding but not border.
1.3. $.outerWidth()、$.outerHeight()
1.4. $.outerWidth(true)、$.outerHeight(true)
Get the current computed outer width (including padding, border, and optionally margin) for the first element in the set of matched elements.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="jquery-1.12.4.js"></script>
<style>
div {
width: 100px;
height: 100px;
padding: 20px;
border: 2px solid blue;
margin: 20px;
background-color: grey;
}
span {
font-size: 15px;
line-height: 10px;
padding: 20px;
border: 2px solid blue;
margin: 20px;
background-color: green;
}
</style>
</head>
<body>
<div id="div1">div1</div>
<div id="div2" style="box-sizing: border-box;">div2</div>
<span id="span1">SPAN</div>
<script>
$(function(){
console.log("div1:width", $("#div1").width(), $("#div1").height());
console.log("div2:width", $("#div2").width(), $("#div2").height());
console.log("span1:width", $("#span1").width(), $("#span1").height());
console.log("div1:innerWidth", $("#div1").innerWidth(), $("#div1").innerHeight());
console.log("div2:innerWidth", $("#div2").innerWidth(), $("#div2").innerHeight());
console.log("span1:innerwidth", $("#span1").innerWidth(), $("#span1").innerHeight());
console.log("div1:outerWidth", $("#div1").outerWidth(), $("#div1").outerHeight());
console.log("div2:outerWidth", $("#div2").outerWidth(), $("#div2").outerHeight());
console.log("span1:outerwidth", $("#span1").outerWidth(), $("#span1").outerHeight());
console.log("div1:outerWidth-true", $("#div1").outerWidth(true), $("#div1").outerHeight(true));
console.log("div2:outerWidth-true", $("#div2").outerWidth(true), $("#div2").outerHeight(true));
console.log("span1:outerwidth-true", $("#span1").outerWidth(true), $("#span1").outerHeight(true));
})
</script>
</body>
</html>
2. 原生
2.1. offsetWidth、offsetHeight
If you need to know the total amount of space an element occupies, including the width of the visible content, scrollbars (if any), padding, and border, you want to use the HTMLElement.offsetWidth and HTMLElement.offsetHeight properties.
2.2. clientWidth、clientHeight
If you need to know how much space the actual displayed content takes up, including padding but not including the border, margins, or scrollbars, you want to use the Element.clientWidth and Element.clientHeight properties.
2.3. scrollWidth、scrollHeight
If you need to know the actual size of the content, regardless of how much of it is currently visible, you need to use the Element.scrollWidth and Element.scrollHeight properties. These return the width and height of the entire content of an element, even if only part of it is presently visible due to the use of scroll bars.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="jquery-1.12.4.js"></script>
<style>
div {
width: 100px;
height: 100px;
padding: 20px;
border: 2px solid blue;
margin: 20px;
background-color: grey;
}
span {
font-size: 15px;
line-height: 10px;
padding: 20px;
border: 2px solid blue;
margin: 20px;
background-color: green;
}
</style>
</head>
<body>
<div id="div1">div1</div>
<div id="div2" style="box-sizing: border-box;">div2</div>
<div id="div3" style="display: none">div3</div>
<div id="div4" style="overflow: hidden; text-overflow: ellipsis;">
Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。
Java可运行于多个平台,如Windows, Mac OS,及其他多种UNIX版本的系统。
本教程通过简单的实例将让大家更好的了解JAVA编程语言。
</div>
<span id="span1">SPAN</div>
<script>
$(function(){
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
var div3 = document.getElementById("div3");
var div4 = document.getElementById("div4");
var span1 = document.getElementById("span1");
console.log("div1:offsetWidth", div1.offsetWidth, div1.offsetHeight);
console.log("div2:offsetWidth", div2.offsetWidth, div2.offsetHeight);
console.log("div3:offsetWidth", div3.offsetWidth, div3.offsetHeight);
console.log("span1:offsetWidth", span1.offsetWidth, span1.offsetHeight);
console.log("div1:clientWidth", div1.clientWidth, div1.clientHeight);
console.log("div2:clientWidth", div2.clientWidth, div2.clientHeight);
console.log("div3:clientWidth", div3.clientWidth, div3.clientHeight);
console.log("span1:clientWidth", span1.clientWidth, span1.clientHeight);
console.log("div1:scrollWidth", div1.scrollWidth, div1.scrollHeight);
console.log("div2:scrollWidth", div2.scrollWidth, div2.scrollHeight);
console.log("div3:scrollWidth", div3.scrollWidth, div3.scrollHeight);
console.log("div4:scrollWidth", div4.scrollWidth, div4.scrollHeight);
console.log("span1:scrollWidth", span1.scrollWidth, span1.scrollHeight);
})
</script>
</body>
</html>
3. 几道笔试题
题目01:
题目02:
参考:
《CSS 权威指南 第四版》 《前端程序员 面试笔试真题与解析》 Determining the dimensions of elements: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Determining_the_dimensions_of_elements Measuring Element Dimension and Location with CSSOM in Windows Internet Explorer 9: https://docs.microsoft.com/en-us/previous-versions//hh781509(v=vs.85)