嗨,我正在制作一个网页,人们在该网页中单击一个按钮并通过 Javascript 保存 cookie,现在它所做的是获取所有 cookie,然后使用单击的那个对其进行排序
任何人都可以有这样的例子或帮助我分类我尝试了一切但它不起作用
就像人们点击一个按钮 - >保存cookie - >然后当用户刷新或再次访问检查cookie并将其显示给用户按钮排序
只是一个代码,示例或想法如何制作这个?
问候, TicTech
var myc=new Array('<td><img class="dmd" src="gray_star.png" id="1" ><a href="#" class="mlm">Name</a></td>','<td><img class="dmd" id="2" src="gray_star.png"><a href="#" class="mlm">Name</a></td>');
function createCookiecreateCookie(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;
}
$(document).ready(function(){
function getCookies() {
var cookies = { };
if (document.cookie && document.cookie != '') {
var split = document.cookie.split(';');
for (var i = 0; i < split.length; i++) {
var name_value = split[i].split("=");
name_value[0] = name_value[0].replace(/^ /, '');
cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
}
}
return cookies;
}
var myCookies = getCookies();
var em=0;
var $user_table = $("#tb");
var $row1 = $("<tr></tr>");
var $row2 = $("<tr></tr>");
var $row3 = $("<tr></tr>");
var $row4 = $("<tr></tr>");
var $row5 = $("<tr></tr>");
var $row6 = $("<tr></tr>");
var $row7 = $("<tr></tr>");
var ar=new Array();
for(m=1;m<=21;m++){
em=readCookie("vale"+m);
if(em==0){
/*
for(i=1;i<=1;i++){
var e=i;
var m="$row";
var $esm=(eval(m+e)).children();
alert($esm.size());
$esm.each(function(){
//alert($(".dmd").attr('src'));});
*/
$row1.prepend(myc[m-1]);
var $esm=$row1.children();
alert($esm.size());}
else
if(m==1||m==2||m==3){
$row1.append(myc[m-1]);
$user_table.append($row1);
var $esm=$row1.children();
$esm.each(function(){
$(".dmd").attr('src');});
}else if(m==4||m==5||m==6){
var $esm=$row1.children();
$esm.each(function(){
$(".dmd").attr('src');});
//alert(m);
$row2.append(myc[m-1]);
$user_table.append($row2);
}else if(m==7||m==8||m==9){
$row3.append(myc[m-1]);
$user_table.append($row3);
}else if(m==10||m==11||m==12){
$row4.append(myc[m-1]);
$user_table.append($row4);
}else if(m==13||m==14||m==15){
$row5.append(myc[m-1]);
$user_table.append($row5);
}else if(m==16||m==17||m==18){
$row6.append(myc[m-1]);
$user_table.append($row6);
}else if(m==19||m==20||m==21){
$row7.append(myc[m-1]);
$user_table.append($row7);
}}
///$e=$row1.children();
//alert($e.size());
//loop through the menu item
$('.dmd').each(function () {
var id=$(this).attr('id');
if(readCookie('vale'+id)==0){
$(this).attr("src","chamkilla.png");
}else{
$(this).attr("src","gray_star.png");
}
});
$("img.dmd").hover(
function(){
var id=$(this).attr('id');
$(this).attr("src","chamkilla.png");
$(this).click(function () {
if(readCookie('vale'+id)==0){
$(this).attr("src","gray_star.png");
createCookiecreateCookie('vale'+id,'1','1');
}else{
createCookiecreateCookie('vale'+id,'0','7');
$(this).attr("src","chamkilla.png");
}});
},
function(){
$(this).attr("src","gray_star.png");
}
);
});