js中的width问题

1、在jQuery中,width()方法用于获得元素宽度;
innerWidth()方法用于获得包括内边界(padding)的元素宽度,
outerWidth()方法用于获得包括内边界(padding)和边框(border)的元素宽度,如果outerWidth()方法的参数为true则外边界(margin)也会被包括进来,即获得包括外边框(margin)、内边界(padding)和边框(border)的元素宽度。
同理,innerHeight方法与outerHeight方法也是用同样的方法计算相应的高度。

width()<=innerWidth()<=outerWidth()<=outerWidth(true);


<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
    var obj=$("#p_obj");
    alert(obj.width());
    alert(obj.innerWidth());
    alert(obj.outerWidth());
    alert(obj.outerWidth(true));
  });
});
</script>

输出的结果是:200,220,240,260.



<p id="p_obj" style=" 200px; padding:10px; border:10px solid blue; margin:10px;">This is a paragraph.</p> <button class="btn1">输出高度</button>

  

原文地址:https://www.cnblogs.com/leyan/p/5112673.html