document.getElementbyId()返回null [英] document.getElementbyId() returning null
问题描述
在标记为//错误的行中的代码中。我收到Javascript错误说:无法读取属性样式'null'。我不知道什么是无法找到的对象。我曾考虑过尝试过一切。请帮忙。以下是代码:
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>
推荐答案
您正在执行您的JavaScript代码, $ c>< div id =connectedUnregistered/> 实际创建。
You are executing your javascript code BEFORE the <div id="connectedUnregistered" />
was actually created.
另请注意,您没有关闭您的< div>
与相应的< / div>
。
Also note that you did not close your <div>
with a corresponding </div>
.
所以将您的JavaScript代码移动到HTML之下的部分。或者在页面加载完成后执行。如果您使用JQuery,您可以执行以下操作:
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>
这篇关于document.getElementbyId()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!