document.getElementbyId() 返回 null

发布于 2024-12-01 05:45:19 字数 942 浏览 0 评论 0原文

在注释中标记为 //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>

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:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

谁与争疯 2024-12-08 05:45:19

您正在实际创建

之前执行 javascript 代码。

另请注意,您没有使用相应的 关闭

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

<script>

    $(document).ready(function() {

        ... your code ...

    });
</script>

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

Also note that you did not close your <div> with a corresponding </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:

<script>

    $(document).ready(function() {

        ... your code ...

    });
</script>
笨死的猪 2024-12-08 05:45:19

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

JavaScript 无法编辑 DOM 元素,因为它尚未创建。

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

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

橘亓 2024-12-08 05:45:19

也许尝试将此代码放在一个

$(document).ready(function(){

//Code

});

块中

Perhaps try placing this code in a

$(document).ready(function(){

//Code

});

block

就像说晚安 2024-12-08 05:45:19

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

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

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

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

<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)

岛歌少女 2024-12-08 05:45:19

另外,如果在代码的某些部分使用 document.write,则很常见得到 null。

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

淡水深流 2024-12-08 05:45:19

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

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.

总攻大人 2024-12-08 05:45:19

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

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

这将在创建 DOM 后执行。

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

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

This will execute after the DOM is created.

凉月流沐 2024-12-08 05:45:19

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

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

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

$(document).ready(function() {
  document.getElementById('connectedUnregistered').style.display = 'block'; //Error
}
蓬勃野心 2024-12-08 05:45:19

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

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

确保您的元素有一个 ID。

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

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

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

Make sure your element has an ID.

<input type="text" name="myelem" id="myelem" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文