<script type="text/javascript">
//封装cookie的操作函数
function setCookie(key, value, expires) {
document.cookie = encodeURIComponent(key) +
'=' + encodeURIComponent(value) + ';expires=' + ddate(expires);
}
function ddate(expires) {
var ddate = new Date();
ddate.setDate(ddate.getDate() + expires)
return ddate;
}
setCookie('name', 'zyy', 1)
setCookie('qq', '222222', 1)
setCookie('mail', '33333', 1)
//读取cookie
function getCookie(o) {
var arrStr = document.cookie.split('; ')
for(var i = 0; i < arrStr.length; i++) {
//alert(arrStr[i])
var arr = arrStr[i].split('=')
if(arr[i] == o) {
return decodeURIComponent(arr[i + 1]);
} else {
return ''
}
}
}
alert(getCookie('name'))
alert(getCookie('qq'))
alert(getCookie('mail'))
</script>name可以返回,而qq和mail返回不了.找到问题了:把return ''去掉就可以.因为return会自动停止循环.
添加回答
举报
0/150
提交
取消