为什么这两个警报显示同一元素的高度和偏右;t显示相同的数字

How come these two alerts showing the height and offsetheight of the same element don't display the same number?

本文关键字:显示 高度 数字 元素 两个 为什么      更新时间:2023-09-26

警报如下:

alert(document.getElementById("col_st_scroll").offsetHeight);
alert(document.getElementById("col_st_scroll").style.height);

现在,col_strongcroll元素在web浏览器中是完全可见的,这难道不意味着offsetHeight和style.height应该返回相同的值吗?

问题解决了。在第一次发出警报时单击"确定"所用的时间内,另一个函数调整了元素的大小。

这来自MDN:

通常,元素的offsetHeight是一个测量值,包括元素边界、元素垂直填充、元素水平滚动条(如果存在,如果渲染)和元素CSS高度。

style.height属性仅表示内容高度(假设您处于"严格"渲染模式,而不是"怪癖")。