Full height of a html element (div) including border, padding and margin?

后端 未结 10 1753
悲&欢浪女
悲&欢浪女 2020-12-01 11:42

I need the full height of a div, I\'m currently using

document.getElementById(\'measureTool\').offsetHeight

offsetHeig

10条回答
  •  旧时难觅i
    2020-12-01 12:03

    old one - anyway... for all jQuery-banning and shortcut folks out there here is some plus scripting which expands the dimension/getabsoluteheight approaches of the other answers:

    function getallinheight(obj) {
      var compstyle=(typeof window.getComputedStyle==='undefined')?obj.currentStyle:window.getComputedStyle(obj);
      var marginTop=parseInt(compstyle.marginTop);
      var marginBottom=parseInt(compstyle.marginBottom);
      var borderTopWidth=parseInt(compstyle.borderTopWidth);
      var borderBottomWidth=parseInt(compstyle.borderBottomWidth);
      return obj.offsetHeight+
             (isNaN(marginTop)?0:marginTop)+(isNaN(marginBottom)?0:marginBottom)+
             (isNaN(borderTopWidth)?0:borderTopWidth)+(isNaN(borderBottomWidth)?0:borderBottomWidth);
    }
    alert(getallinheight(document.getElementById('measureTool')));
    

提交回复
热议问题