我试图隐藏随机的多个元素,所以我所做的就是给每个元素一个唯一的ID。
$(function() {
var left= $('#left'),
right = $('#right'),
heads = $('.maintitle'),
i,
leftHead = $(left).find(heads),
rightHead = $(right).find(heads);
$(leftHead).prepend('<div class="close"></div>');
$(rightHead).prepend('<div class="close"></div>');
var close = $('.close'),
Lnumber = $('#left .module'),
Rnumber =$('#right .module');
for (i=0;i<Lnumber.length;i++) {
$(Lnumber)[i].id='widg'+i;
}
close.click(function() {
var id = $(this).parent().parent().attr('id');
$(this).parent().parent().slideUp().addClass('hidden');
SetCookie('hidden'+id,"true",100);
});
var cookieId = $('#left .module');
for (i=0;i<cookieId.length;i++) {
var newArray = [cookieId[i].id];
if (ReadCookie(newArray) == 'true') {
var cName = newArray.replace('hidden','');
alert(cName);
}
}
});
正如你在ReadCookie函数中看到的那样,我让它警告隐藏的ID,尽管这还不起作用。我是Cookies的新手,并且很难让它正常工作。
以下是Cookie的功能......
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString(),';path = /';
}
function KillCookie(cookieName) {
SetCookie(cookieName,"", - 1);
}
function ReadCookie(cookieName) {
var theCookie=""+document.cookie;
var ind=theCookie.indexOf(cookieName+"=");
if (ind==-1 || cookieName=="") return "";
var ind1=theCookie.indexOf(";",ind);
if (ind1==-1) ind1=theCookie.length;
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
如果有人看到任何其他方法可以做到这一点很好,只需要获取特定的ID,以确保在点击和更改页面时它们保持隐藏状态。
答案 0 :(得分:0)
您是否尝试过jQuery Cookie插件?
https://github.com/carhartl/jquery-cookie
然后创建过期的cookie,在整个网站上有效:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
然后阅读cookie:
$.cookie('the_cookie'); // => "the_value"