日期:2014-05-16  浏览次数:20436 次

cookie记录列表
我需要一个临时列表的功能, 比如像现在一些视频网站的播放历史记录这样, 不过我想要的是:左边是一个列表,列表里有几条数据, 比如这样,

左边是列表,点击+将此条数据添加到cookie,右边实时显示,然后右边取得的cookie点击-号可以删除。


请大神们帮帮忙,菜鸟在此先谢了~~~

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var input=document.getElementsByTagName("input");
for(var i=0;i<input.length;i++){
if(input[i].type=="button"){
input[i].style.backgroundColor="white";
input[i].style.border="none";
if(input[i].value=="+"){
input[i].onclick=addcookie;
}else if(input[i].value=="-"){
input[i].onclick=removecookie;
}
}
}

function addcookie(){
var li=this.previousSibling;
var cookie=document.cookie;
var begin=cookie.indexOf("list");
if(begin!=-1){
var end=cookie.indexOf(";",begin);
if(end==-1){
end=cookie.length;
}
var c=cookie.substring(begin+5,end);
if(c.indexOf(li.innerHTML)==-1){
c+=","+li.innerHTML;
document.cookie="list="+c+";max-age=100000";
}
}else{
document.cookie="list="+li.innerHTML+";max-age=100000";
}
alert("addcookie:"+document.cookie);
show();
}
function removecookie(){
var li=this.previousSibling;
var cookie=document.cookie;
var begin=cookie.indexOf("list");
if(begin!=-1){
var end=cookie.indexOf(";",begin);
if(end==-1){
end=cookie.length;
}
var c=cookie.substring(begin+5,end);
c=c.split(",");
for(var i in c){
if(c[i]==li.innerHTML){
delete c[i];
}
}
c=c.toString();
document.cookie="list="+c+";max-age=100000";
}
alert("removecookie"+document.cookie);
show();
}
function show(){
var ul=document.getElementById("test");
var sun=ul.childNodes;
var l=sun.length;
for(var i=0;i<l;i++){
ul.removeChild(sun[0]);
}
var cookie=document.cookie;
var begin=cookie.indexOf("list");
if(begin!=-1){
var end=cookie.indexOf(";",begin);
if(end==-1){
end=cookie.length;
}
var c=cookie.substring(begin+5,end);
c=c.split(",");
for(var i in c){
if(c[i]){
var li=document.createElement("li");
var a=document.createElement("a");
a.innerHTML=c[i];
var input=document.createElement("input");
input.type="button";
input.value="-";
input.style.backgroundColor="white";
input.style.border="none";
li.appendChild(a);
li.appendChild(input);
ul.appendChild(li);
}
}
}
init();
}
function deletecookie(){
document.cookie="list=";
}
window.onload=function(){init();deletecookie();}
</script>
</head>

<body>
<table>
<th>列表</th>
<th>显示</th>
<tr>
<td>
<ul>
<li><a>name1</a><input type="butto