html - 如何从html div元素获取高度?

标签 html typescript angular5

我正在使用 Angular 5,在组件中我有一个方法

onFullScreen : function (event){ console.log(event[0]) }

在这里,当我执行console.log(event[0])时,这将返回这个

enter image description here

这会返回一个 HTMLDivElement,现在我想在 onFullScreen() 方法中获取高度属性。如何获得?

最佳答案

获取元素高度(以及许多其他布局相关属性)的最佳方法是使用 getBoundingClientRect ( https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect )。

所以你可以这样做:

const height = event[0].getBoundingClientRect().height;

关于html - 如何从html div元素获取高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49726852/

相关文章:

CSS水平绝对位置和垂直相对位置

javascript - 在 Monaco Editor 中滚动到一行

Angular 5 通过组件传递 ngModel

Javascript 将 html 粘贴到自身中?

jQuery UI Resizable with overflow-y

reactjs - 用样式组件和 typescript 覆盖 Prop

javascript - 为什么我在 querySelectorAll 上收到错误 TS2339,因为复选框属性 'checked' 不存在

angular - RxJS 检测订阅何时关闭

Angular 5,RxJs { map } 导入不起作用或者我丢失了一些东西?

html - 定位图像与文字一致?