javascript - 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
解决方案
推荐阅读
- python - django-admin正在执行,但virual目录中没有创建项目
- apache-kafka - 回复 Kafka 模板 - 异常处理
- amazon-cloudformation - 在 ECS 中使用应用程序负载均衡器的 504 网关超时
- django - 如何在表单中定义电话号码和模板错误获取:django.template.exceptions.TemplateDoesNotExist:phone_field/phone_widget.html?
- c# - Postman - 如何使用 V2 端点将用户导入 BIM 360 项目
- c++ - C++ 中的错误消息:函数/变量“未在此范围内声明”,变量“是私有的”
- reactjs - 如果文本值中有表情符号,则 TextInput 样式不会更新
- firebase - Firebase认证,IOS导致PlatformException
- r - 在 r 的范围内查找值并将值的数量相加
- java - Springboot -> Springboot 2:在类路径上检测到 2 个 Spring WebApplicationInitializers