2017-09-08 31 views
0

好日子大家,的getElementById返回null(因爲還沒有加載)

我試圖獲得其在javascript ID的元素,並將其返回我null

實際上,當DOM由於插件Restrict Content Pro(其中目標元素來自)在頁面加載後發出請求(請參閱下面的參考圖像)時,此元素未完全加載。

我寫的臨時解決方案基本上是一個2秒setTimeOutgetElementByID函數,這是一種...殘酷。 ☺

如果插件的加載時間超過2秒,則不起作用。

所以,我的問題是:你知道有什麼方法可以正確地做到嗎?

喜歡的東西

- while returns null try to get it - when returns element stop the loop

好知道document.onloadwindow.onlaod將無法​​正常工作,因爲頁面後插件加載元素充分展示。

在此先感謝!

When the page is loaded, the plugin is still displaying elements (Restrict Content Pro plugin with Stripe)

+0

[MutationObserver(https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) –

+0

是否就像「當DOM中的某些內容發生變化時,請執行功能「? –

+0

我不知道,我看不懂 –

回答

0

我的解決方案:

do { 
    var getId = getElementByID(); 
    if(getID!=null){ 
    // to do 

    } 
} 
while (getId==null);