標籤:
width():
不包括元素的外邊距(margin)、內邊距(padding)、邊框(border)等部分的寬度。
innerWidth():
包括元素的內邊距(padding),但不包括外邊距(margin)、邊框(border)等部分的寬度。
outerWidth():
包括元素的內邊距(padding)、邊框(border),但不包括外邊距(margin)部分的寬度。你也可以指定參數為true,以包括外邊距(margin)部分的寬度。
1 <body>2 <div class="box"></div>3 </body>
1 <style>2 .box{ width: 100px;height: 100px;background-color: red; }3 </style>
1 <script> 2 $(function () { 3 //無邊距 4 console.log("width",$(".box").width());//100px 5 console.log("innerWid", $(".box").innerWidth());//100px 6 console.log("outerWid",$(".box").outerWidth());//100px 7 //加padding 10px; 8 $(".box").css("padding", "10px"); 9 console.log("width", $(".box").width());//100px10 console.log("innerWid", $(".box").innerWidth());//120px11 console.log("outerWid", $(".box").outerWidth());//120px12 //加border 5px13 $(".box").css("border", "5px solid orange");14 console.log("width", $(".box").width());//100px15 console.log("innerWid", $(".box").innerWidth());//120px16 console.log("outerWid", $(".box").outerWidth());//130px17 //加margin 10px18 $(".box").css("margin", "10px");19 console.log("width", $(".box").width());//100px20 console.log("innerWid", $(".box").innerWidth());//120px21 console.log("outerWid", $(".box").outerWidth());//130px22 console.log("outerWid", $(".box").outerWidth(true));//150px23 })24 </script>
jQuery的width()、innerWidth()、outerWidth()方法