源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ var txt=""; txt+="div 寬度: " + $("#div1").width() + "</br>"; txt+="div 高度: " + $("#div1").height() + "</br>"; txt+="div 寬度,包含內邊距和邊框: " + $("#div1").outerWidth() + "</br>"; txt+="div 高度,包含內邊距和邊框: " + $("#div1").outerHeight(); $("#div1").html(txt); }); }); </script> </head> <body> <div id="div1" style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div> <br> <button>顯示 div 元素的尺寸</button> <p>outerWidth() - 返回元素的寬度 (包含內邊距和邊框)。</p> <p>outerHeight() - 返回元素的高度 (包含內邊距和邊框)。</p> </body> </html>
運行結果