簡體   English   中英

document.getElementById(“ componentID”)。clientHeight在componentDidMount()函數中始終為零

[英]document.getElementById(“componentID”).clientHeight always give a zero in componentDidMount() function

我想在頁面完全加載后獲取標題的高度。

我試圖通過調用在componentDidMount()狀態中保存高度

document.getElementById("Header").clientHeight

但它始終在componentDidMount()中返回0。 我也試圖打電話

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

但也因為.getClientRects()返回空數組而失敗。 這兩種方法可以在componentDidMount()之外的任何其他函數中使用

您是否嘗試將其包裝為超時?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM