0

对此感到困惑......所以我有一个按钮,点击后会显示一个隐藏的 div。我试图设置一个cookie onlick,以便即使在页面重新加载后div也会显示,但我没有运气......

我不明白的是如何获得点击状态的值......

例如:

setCookie('CookieName', 'What goes here for the clicked button state value?', 'LENGTH OF COOKIE LIFE')

<html>
<head>

<script type="text/javascript" language="JavaScript">

function SetCookie() {
if(arguments.length < 2) { return; }
var n = arguments[0];
var v = arguments[1];
var d = 0;
if(arguments.length > 2) { d = parseInt(arguments[2]); }
var exp = '';
if(d > 0) {
    var now = new Date();
    then = now.getTime() + (d * 24 * 60 * 60 * 1000);
    now.setTime(then);
    exp = '; expires=' + now.toGMTString();
    }
document.cookie = n + "=" + escape(String(v)) + '; path=/' + exp;
} // function SetCookie()

function ReadCookie(n) {
var cookiecontent = new String();
if(document.cookie.length > 0) {
    var cookiename = n+ '=';
    var cookiebegin = document.cookie.indexOf(cookiename);
    var cookieend = 0;
    if(cookiebegin > -1) {
        cookiebegin += cookiename.length;
        cookieend = document.cookie.indexOf(";",cookiebegin);
        if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
        cookiecontent = document.cookie.substring(cookiebegin,cookieend);
        }
    }
return unescape(cookiecontent);
} // function ReadCookie()
// —&gt;
</script>


<style type="text/css">
#sub3 {
position: absolute;
left: 100px;
top: 200px;
background-color: #f1f1f1;
width: 180px;
padding: 10px;
color: black;
border: #0000cc 2px dashed;
display: none;
}
</style>

<script language="JavaScript">
function setVisibility(id, visibility) {
document.getElementById(id).style.display = visibility;
}
</script>

</head>
<body >

<script language="JavaScript">
function clicked() {
    setVisibility(id, visibility);
    setCookie() // this calls your set cookie function
    return true;
}
</script>

<input id="input1" type=button value='Show Layer' onclick="clicked();">

<div id="sub3">Message Box</div>

</body>
</html>
4

1 回答 1

0

您的函数setVisibility(id, visibility)接受 2 个属性,您需要指定值,例如setVisibility('sub3', 'block')是否要显示您的块。

您需要将参数传递给您的函数SetCookie(name, value)GetCookie(name).

GetCookie(name)如果函数返回值,您还需要在页面加载事件中显示您的块。

于 2012-11-17T10:51:23.380 回答