使用纯JavaScript获取div高度

lwi*_*iii 287 html javascript css

关于如何在不使用jQuery的情况下获得div高度的任何想法?

我正在搜索Stack Overflow这个问题,似乎每个答案都指向jQuery .height().

我尝试了类似的东西myDiv.style.height,但它什么也没有返回,即使我的div有它width并且height设置在CSS中.

Dan*_*Dan 538

var clientHeight = document.getElementById('myDiv').clientHeight;
Run Code Online (Sandbox Code Playgroud)

要么

var offsetHeight = document.getElementById('myDiv').offsetHeight;
Run Code Online (Sandbox Code Playgroud)

clientHeight 包括填充.

offsetHeight 包括填充,滚动条和边框.

  • `clientHeight`也包含填充,因此它不等于`$ .height()` (4认同)
  • 注意:**OP没有要求排除填充**的答案.对于我所知道的,没有原生的跨平台解决方案. (3认同)
  • offsetheight不会在ie10以下的网站上发现http://www.quirksmode.org/mobile/tableViewport_desktop.html (2认同)
  • 您还可以使用`scrollHeight`,其中包含所包含文档的高度,垂直填充和垂直边框. (2认同)
  • 边界和边距也占据了空间.:)你的回答暗示`clientHeight`不包括填充.问题提到了`$ .height()`和`.style.height`,两者都不包括填充,暗示提问者也不想要它. (2认同)

Dan*_*mms 21

的jsfiddle

var element = document.getElementById('element');
alert(element.offsetHeight);
Run Code Online (Sandbox Code Playgroud)


use*_*883 19

另一种选择是使用getBoundingClientRect函数.请注意,如果元素的显示为"none",则getBoundingClientRect将返回一个空矩形.

例:

var elem = document.getElementById("myDiv");
if(elem) {
   var rect = elem.getBoundingClientRect();
   console.log(rect.height);  
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为这是更好的解决方案 (2认同)

Sah*_*mar 18

  1. HTMLElement.style - 返回元素的定义样式
  2. clientHeight - 高度 + 填充
  3. offsetHeight - 高度 + 内边距 + 边框
  4. scrollHeight - 返回高度+内边距
  5. getBoundingClientRect() - 返回左、上、右、下、x、y、宽度和高度属性
  6. getCompatedStyle() - 返回元素的所有 CSS 属性

// returns defined Style of element
const styleHeight = document.getElementById('myDiv').style.height;
console.log('style Height : ', styleHeight);

// height + padding
const clientHeight = document.getElementById('myDiv').clientHeight;
console.log('client Height : ', clientHeight);

// height + padding + borders
const offsetHeight = document.getElementById('myDiv').offsetHeight;
console.log('offset Height : ', offsetHeight);

// height + padding
const scrollHeight = document.getElementById('myDiv').scrollHeight;
console.log('scroll Height : ', scrollHeight);

// return left, top, right, bottom, x, y, width, and height properties
const getBoundingClientRectHeight = document.getElementById('myDiv').getBoundingClientRect().height;
console.log('get Bounding Client Rect Height : ', getBoundingClientRectHeight);

// returns all CSS properties of an element
const styleElementHeight = getComputedStyle(document.getElementById("myDiv")).height;
console.log('style Element Height : ', styleElementHeight);
Run Code Online (Sandbox Code Playgroud)
<div id="myDiv" style="margin:10px;padding:20px;height:200px;">
    <input type="Text">
    <input type="button" value="submit">
</div>
Run Code Online (Sandbox Code Playgroud)


Keo*_*ife 10

var myDiv = document.getElementById('myDiv'); //get #myDiv
alert(myDiv.clientHeight);
Run Code Online (Sandbox Code Playgroud)

clientHeight和clientWidth是您正在寻找的.

offsetHeight和offsetWidth也返回高度和宽度,但它包括边框和滚动条.根据具体情况,您可以使用其中一种.

希望这可以帮助.


cra*_*ned 6

其他答案对我不起作用。这是我在w3schools找到的内容,假设div有一个height和/或width集合。

您所需要的height只是width排除填充。

    var height = document.getElementById('myDiv').style.height;
    var width = document.getElementById('myDiv').style.width;
Run Code Online (Sandbox Code Playgroud)