日期:2014-05-16 浏览次数:20440 次
<ul id="level"> <li>0</li> <li>0</li> <li>0</li> <li>0</li> </ul> <script type="text/javascript"> function setCookie(name,value,date){ value=escape(value); var nowDate=new Date(); nowDate.setMonth(nowDate.getMonth()+date); cookieDate=nowDate.toGMTString(); document.cookie=name+'='+value+';expires='+cookieDate; } function getCookie(name){ var str=document.cookie; var arr=str.split('; '); var i=0; for(i=0; i<arr.length; i++){ var arr2=arr[i].split('='); if(arr2[0]==name){ return arr2[1]; } } } window.onload=function(){ var element=document.getElementById('level'); var lis=element.getElementsByTagName('li'); var i=0; var lisValue=[]; var cookieValue=getCookie('lisValue'); if(cookieValue){ var cookieValue2=cookieValue.split('') for(i=0; i<cookieValue2.length; i++){ lis[i].innerHTML=cookieValue2[i]; } }else{ for(i=0; i<lis.length; i++){ lisValue.push(lis[i].innerHTML); } } for(i=0; i<lis.length; i++){ lis[i].index=i; lis[i].onclick=function(){ this.innerHTML=parseInt(this.innerHTML)+1; lisValue.splice(this.index,1,this.innerHTML); var strCookie=lisValue.join(''); setCookie('lisValue',strCookie,9); alert(document.cookie); } } } </script>
for(i=0; i<lis.length; i++){ lis[i].index=i; lis[i].onclick=function(){ this.innerHTML=parseInt(this.innerHTML)+1; lisValue.splice(this.index,1,this.innerHTML); var strCookie=lisValue.join(''); setCookie('lisValue',strCookie,9); alert(document.cookie); } }
------解决方案--------------------