jquery包括margin外邊距的outerWidth或者outerHeight

antzone發表於2017-04-03

本章節分享一段程式碼例項,它實現了獲取包括外邊距在內的outerWidth或者outerHeight效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
#antzone{
  height:100px;
  width:300px;
  padding:10px;
  margin:3px;
  border:1px solid blue;
  background-color:lightblue;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$(document).ready(function(){
  $("#bt").click(function(){
    var txt="";
    txt+="div的寬度:" + $("#antzone").width()+"</br>";
    txt+="div的高度:" + $("#antzone").height()+"</br>";
    txt+="包括margin的outerWidth:"+$("#antzone").outerWidth(true)+"</br>";
    txt+="包括margin的outerHeight:"+$("#antzone").outerHeight(true);
    $("#antzone").html(txt);
  });
});
</script>
</head>
<body>
<div id="antzone"></div>
<input type="button" id="bt" value="檢視效果"/>
</body>
</html>

outerWidth()方法可以參閱jQuery outerWidth()一章節。

相關文章