document.getElementById有时会返回null [英] document.getElementById sometimes returns null

查看:82
本文介绍了document.getElementById有时会返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一行代码在页面的onLoad事件中调用,并且工作正常.在onload事件内部,调用了其他函数,现在尝试同一行代码将返回null.

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.

逐字编码

riskInStateInputTypeElement = document.getElementById("riskInStateInputType");

在以前返回正确的东西之后,getElementById是否有开始返回null的实例?

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

行和不行都在同一个调用堆栈中,而不是分开的事件或线程. Document readyStatus两次都完成",所以我认为这与页面未完全加载有关.有什么想法我应该看看吗?

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?

推荐答案

您是否检查过它是否指向具有该ID的元素?也许您在<div></div>(或该id所指的任何内容)中拼错了id="riskInStateInputType".

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?

这篇关于document.getElementById有时会返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆