document.getElementById sometimes returns null

南楼画角 提交于 2020-01-15 09:31:54

问题


I have a line of code that is called in the onLoad event of a page, and it works fine. Inside the onload event, other functions are called, and trying the same line of code now returns null.

Code verbatim

riskInStateInputTypeElement = document.getElementById("riskInStateInputType");

Are there any instances where getElementById begins returning null after having returned the correct thing previously?

The line that works and the line that doesn't work are both in the same call stack, not separate events or threads. The Document readyStatus is 'complete' both time, so I dont think its a matter of the page not entirely loaded. Any ideas where I should look?


回答1:


Did you check that it's referring to an element with that id? Maybe you misspelled id="riskInStateInputType" in your <div></div> (or whatever that id is referring to).

Are you deleting the content within that element before you call it the second time perchance?



来源:https://stackoverflow.com/questions/17302111/document-getelementbyid-sometimes-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!