首页 > 解决方案 > getBoundingClientRect 和 offsetHeight 问题获取高度

问题描述

我试图在插入其 textContent 后获取元素的高度。当文本超过 2 行时,元素的高度会发生变化。问题是第一次点击我得到的高度是错误的,前面的点击是正确的高度。

// onclick
element.textContent = text

var byOffset = element.offsetHeight;
var byBound = element.getBoundingClientRect();

console.log('by offset height', byOffset);
console.log('by bound height', byBound.height);

第一次点击

console: by offset height 37
console: by bound height 37

在前面的点击(首选输出)

console: by offset height 79
console: by bound height 79

标签: javascript

解决方案


推荐阅读