JavaScript:未捕获的类型错误:无法读取 null 的属性“样式”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11792794/
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
JavaScript: Uncaught TypeError: Cannot read property 'style' of null
提问by Chlo? Vd Eeden
I am building this small cookiescript which creates a cookie when clicking on a button and then hides the message.
我正在构建这个小 cookiescript,它在单击按钮时创建一个 cookie,然后隐藏消息。
But now I am building the function to hide the message (div#cookie) when the cookie has been seet, but I get this error everytime, but my div DOES exist:
但是现在我正在构建函数以在 cookie 被看到时隐藏消息 (div#cookie),但我每次都会收到此错误,但我的 div 确实存在:
Uncaught TypeError: Cannot read property 'style' of null
Now these are the scripts I am using, can anyone help? :)
现在这些是我正在使用的脚本,有人可以帮忙吗?:)
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>
and this, where-ever the 'hidediv' gives the error, but does work when clicking on a button:
而这个,无论“hidediv”在哪里给出错误,但在单击按钮时确实有效:
<script type="text/javascript">
function hidediv() {
if (document.getElementById) {
document.getElementById('cookie').style.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',365)
}
else {
if (document.layers) {
document.hideShow.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',356)
}
else {
document.all.hideShow.style.visibility = 'hidden';
createCookie('uscnCookieScriptJS','uscninternetservicescookiescriptjavascriptversion',356)
}
}
}
function showdiv() {
if (document.getElementById) {
document.getElementById('cookie').style.visibility = 'visible';
}
else {
if (document.layers) {
document.hideShow.visibility = 'visible';
}
else {
document.all.hideShow.style.visibility = 'visible';
}
}
}
</script>
<script type="text/javascript">
if (document.cookie.indexOf("uscnCookieScriptJS") >= 0) {
alert("yes");
hidediv();
}
else {
alert("no");
}
</script>
The script can be found here: http://dev.uscn.nl/cookiescript/v2/
该脚本可以在这里找到:http: //dev.uscn.nl/cookiescript/v2/
回答by Maxim Krizhanovsky
The problem is that you are calling the hidediv()
function before loading the actual HTML content. Either put the script in the end of the document (before closing body
tag) or set it at window.onload
/ on document ready
问题是您hidediv()
在加载实际 HTML 内容之前调用了该函数。将脚本放在文档的末尾(在关闭body
标签之前)或将其设置为window.onload
/ on document ready