gpt4 book ai didi

html - document.getElementById ("componentID").clientHeight 总是在 componentDidMount() 函数中给出零

转载 作者:行者123 更新时间:2023-11-28 02:24:01 26 4
gpt4 key购买 nike

我想在页面准确加载后获取页眉的高度。

我试图通过调用将高度保存在 componentDidMount() 中的状态

document.getElementById("Header").clientHeight

但它总是在 componentDidMount() 中返回 0。我也试着打电话

document.getElementById("Header").getClientRects()[0].height

但它也失败了,因为 .getClientRects() 返回空数组。这两种方法适用于 componentDidMount() 以外的任何其他函数

最佳答案

你试过超时包装吗?

componentDidMount() {
setTimeout(()=>{
document.getElementById("Header").clientHeight
},0)
}

关于html - document.getElementById ("componentID").clientHeight 总是在 componentDidMount() 函数中给出零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55549683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com