1

对不起,我是 JavaScript 的新手。display:none如果 are_cookies_enabled() 函数返回 false,我想要一个当前具有取消隐藏 onload 的 div 。但是,我的 div 并不是这样说的。如果我将它封装在 jquery onload 中,它本身就可以工作。我究竟做错了什么?

注意 noCookies 中的函数设置为 true (这只是为了查看取消隐藏功能是否有效,因为我启用了 cookie)。

    function are_cookies_enabled()
    {
        var cookieEnabled = (navigator.cookieEnabled) ? true : false;

        if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
        { 
            document.cookie="testcookie";
            cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
        }
        return (cookieEnabled);
    }
    function noCookies()
    {
        if (are_cookies_enabled() == true) {
            document.getElementById('nocookie').style.display='block';
        }
    }
    window.onload = noCookies();

分区:

<div id="nocookie" style="display:none;"><div class="ec-messages messages-warning">This site may not function properly if cookies are disabled. Session variables are stored in cookies. Please enable cookies.</div></div>
4

1 回答 1

0

尝试更改window.onload = noCookies()window.onload = noCookies.

window.onload是一个处理“加载”事件 [ ref ] 的事件处理程序,因此如果您将noCookies函数分配给window.onload,则该函数将在“加载”事件触发时被调用。

但是,在您的代码中,函数的返回值(未定义)被分配给window.onload,毫无疑问它不会工作。

于 2013-06-12T00:46:09.300 回答