javascript div状态更改+ cookie

时间:2014-08-31 20:30:31

标签: javascript cookies

嗨,我有新的JavaScript,我有一个问题。

我找到了这段代码并稍微改了一下。

<head>
<script type="text/javascript">
name='open';
function toggle(showHideDiv, switchImgTag) {
        var ele = document.getElementById(showHideDiv);
        var imageEle = document.getElementById(switchImgTag);
        if(ele.style.display == "block") {
                ele.style.display = "none";
                createCookie(name,'false',0);
                imageEle.innerHTML = 'Show this window';
        }
        else {
                ele.style.display = "block";
                createCookie(name,'true',0);
                imageEle.innerHTML = 'Hide this window';
        }
}
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 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 checkCookie(name) {
    var x = readCookie(name)
    if (x == 'false') {
        document.getElementById('showHideDiv').style.display = 'none'
    } else if (x == 'true') {
        document.getElementById('showHideDiv').style.display = 'block';
    }
}
</script>
<style type="text/css">
body
{
    width: 300px;
}
</style>
</head>
<body onload="javascript:checkCookie('open')">
<button type="button" style="float:right;" onclick="toggle('showHideDiv');">click here</button>
<div id="showHideDiv" style="display:block;">Some bs to hide or not</div>
</body>

现在这个工作正常我觉得......但是对于上帝的爱我无法弄清楚如何改变饼干长度(以天为单位)我甚至不知道他们现在的日志如何...可以请一些人帮助我只是这个小东西? :)提前谢谢!

1 个答案:

答案 0 :(得分:0)

看一下jQuery,它为您提供了许多方法,可以让您的JavaScript生活更轻松,更轻松......