This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
                                
                                    (7个答案)
                                
                        
                                3年前关闭。
            
                    
我正在构建这个小的cookiescript,当单击按钮时会创建一个cookie,然后隐藏消息。

但是现在我正在构建一个函数,以在发现cookie时隐藏消息(div#cookie),但是每次都会出现此错误,但是我的div确实存在:

Uncaught TypeError: Cannot read property 'style' of null


现在这些是我正在使用的脚本,有人可以帮忙吗? :)

<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'何时出现错误,但单击按钮时都可以工作:

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

最佳答案

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

关于javascript - JavaScript:未捕获的TypeError:无法读取null的属性'style',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11792794/

10-11 15:37