JavaScript:Uncaught TypeError:无法读取属性“style”为null [英] JavaScript: Uncaught TypeError: Cannot read property 'style' of null

查看:182
本文介绍了JavaScript:Uncaught TypeError:无法读取属性“style”为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立这个小饼干文件,当点击按钮时创建一个cookie,然后隐藏消息。

I am building this small cookiescript which creates a cookie when clicking on a button and then hides the message.

但现在我建立的函数来隐藏消息(div#cookie)当cookie已被seet,但我得到这个错误,每次,但我的div存在:

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:

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>

这里,'hidediv'出现错误,按钮:

and this, where-ever the 'hidediv' gives the error, but does work when clicking on a button:

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

脚本可以在这里找到: http://dev.uscn.nl/cookiescript/v2/

The script can be found here: http://dev.uscn.nl/cookiescript/v2/

推荐答案

问题是你在调用 hidediv()函数之前加载实际的HTML内容。将脚本放在文档的结尾(在关闭 body 标记之前)或将其设置为 window.onload on document ready

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

这篇关于JavaScript:Uncaught TypeError:无法读取属性“style”为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆