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

为什么cookie获取回来,再存进去的值的位置就不对了~~,请教呀~~
JScript code

<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>




------解决方案--------------------
C/C++ code
    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);
        }
    }

------解决方案--------------------
探讨

C/C++ code
for(i=0; i<lis.length; i++){
lis[i].index=i;
lis[i].onclick=function(){
this.innerHTML=parseInt(this.innerHTML)+1;


li……