Javascript document.getElementbyId() 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7151904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:04:10  来源:igfitidea点击:

document.getElementbyId() returning null

javascriptdom

提问by Umair Khan Jadoon

In the code at the line marked as //Error in comments. I get Javascript error saying: "Cannot read property style of 'null'". I have no idea what it is not able to find the object. I have thought and tried everything. Please help. Here is the code:

在注释中标记为 //Error 的行的代码中。我收到 Javascript 错误消息:“无法读取 'null' 的属性样式”。我不知道是什么东西找不到对象。我想过并尝试过一切。请帮忙。这是代码:

<script type="text/javascript">

    $.getJSON('https://graph.facebook.com/647045111?fields=first_name&<%= Session["Token"] %>',

    function (data) {
        $('#userName').html(data.first_name);
    });

    document.getElementById('connectedUnregistered').style.display = 'block'; //Error

</script>

    <div id="userName"></div>

    <div id="disconnected" style="display:block;">

     <div id="heading">Facebook login</div> 

     <a href="Account/FacebookLogin" id="loginButton"><div id="fbConnectButton"><img src="/Content/Images/fbconnect.png"/></div></a>    

    </div>

     <div id="connectedUnregistered" style="display:none">

     <div id="heading">Register Now</div> 



    </div>

回答by Jules

You are executing your javascript code BEFORE the <div id="connectedUnregistered" />was actually created.

您正在<div id="connectedUnregistered" />实际创建之前执行您的 javascript 代码。

Also note that you did not close your <div>with a corresponding </div>.

另请注意,您没有<div>使用相应的</div>.

So move your javascript code to a part below your HTML. Or execute it after the page finished loading. If you are using JQuery you can do:

因此,将您的 javascript 代码移至 HTML 下方的部分。或者在页面加载完成后执行。如果您使用的是 JQuery,您可以执行以下操作:

<script>

    $(document).ready(function() {

        ... your code ...

    });
</script>

回答by Blazemonger

Put your script at the end of the HTML document instead of the beginning, and see if that solves things.

将您的脚本放在 HTML 文档的末尾而不是开头,看看是否能解决问题。

JavaScript can't edit the DOM element because it hasn't been created yet.

JavaScript 无法编辑 DOM 元素,因为它还没有被创建。

回答by Fareesh Vijayarangam

Perhaps try placing this code in a

也许尝试将此代码放在

$(document).ready(function(){

//Code

});

block

堵塞

回答by Heitor Colangelo

Also, if in some part of the code you are using document.writeit's very common to get null.

此外,如果在您使用的代码的某些部分中document.write,获得 null 是很常见的。

回答by live-love

This error could also indicate that the element doesn't have an ID.

此错误还可能表示该元素没有 ID。

<input type="text" name="myelem" />

Make sure your element has an ID.

确保您的元素具有 ID。

<input type="text" name="myelem" id="myelem" />

回答by Dogugun Ozkaya

in my case it was because of having this line at the beginning of the jsp/html(whatever) file:

就我而言,这是因为在 jsp/html(whatever) 文件的开头有这一行:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

removing it solved the problem for me(I do not remember what it was doing on my page at first place)

删除它为我解决了问题(我不记得它首先在我的页面上做什么)

回答by James Montagne

The code executes prior to the element being created. Since you are using jquery, simply wrap it in document.ready:

代码在创建元素之前执行。由于您使用的是 jquery,只需将其包装在 document.ready 中:

$(function(){
    // code goes here
});

This will execute after the DOM is created.

这将在创建 DOM 后执行。

回答by JaredPar

The javascript code is running before the DOM is fully available and the call fails. Try the following instead

在 DOM 完全可用并且调用失败之前,javascript 代码正在运行。请尝试以下操作

$(document).ready(function() {
  document.getElementById('connectedUnregistered').style.display = 'block'; //Error
}

回答by FishBasketGordo

The script is trying to get the element before the element is loaded. Place the script after the element or put it in a load or ready event.

脚本试图在加载元素之前获取元素。将脚本放在元素之后或将其放在加载或就绪事件中。