admin管理员组文章数量:1419666
(function() {
var myFunc = {
init : function() {
alert("I need");
document.getElementById("myDiv").innerHTML = "help";
}
};
window.myFunc = myFunc;
})();
myFunc.init();
The alert() message works but the Uncaught TypeError is thrown when it reaches the next line. Can anyone explain to me why and how to fix it? I assume it is something to do with the scoping of the function as it will work if nested within the myDiv rather than the linked .js file.
Thanks
(function() {
var myFunc = {
init : function() {
alert("I need");
document.getElementById("myDiv").innerHTML = "help";
}
};
window.myFunc = myFunc;
})();
myFunc.init();
The alert() message works but the Uncaught TypeError is thrown when it reaches the next line. Can anyone explain to me why and how to fix it? I assume it is something to do with the scoping of the function as it will work if nested within the myDiv rather than the linked .js file.
Thanks
Share Improve this question asked Nov 16, 2010 at 16:35 neiler45neiler45 31 silver badge2 bronze badges 2- Nothing looks wrong with your code. Which browser is it failing in? I tried it in FF4, IE8, and Chrome9. They all worked. jsfiddle/PffYx – James Kovacs Commented Nov 16, 2010 at 16:40
- its in chrome. the alert works fine which is why i cant understand why it gets that far but wont set the innerHTML – neiler45 Commented Nov 16, 2010 at 16:51
2 Answers
Reset to default 5Your script is executing before the document is parsed.
You should move the <script>
tag to the bottom of the document, or call init
in the onload
event.
is document.getElementById("myDiv") returning null?
本文标签:
版权声明:本文标题:javascript - can someone explain to me why i get an Uncaught TypeError: cannot set property of null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745301046a2652372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论