<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题pj文档</title> <style> </style> <script> window.onload=function() { var oText=document.getElementById('username') var oLogin=document.getElementById('btn1'); var oBtn=document.getElementById('btn2'); if ( getCookie('username') ) { oText.value = getCookie('username'); } oLogin.onclick = function() { alert('登陆成功'); setCookie('username', oText.value, 5); } oBtn.onclick=function() { removeCookie('username'); oText.value=''; } }; function setCookie(key, value, t) { var oDate = new Date(); oDate.setDate( oDate.getDate() + t ); document.cookie = key + '=' + value + ';expires=' + oDate.toGMTString(); } function getCookie(key) { var arr1 = document.cookie.split('; '); for (var i=0; i<arr1.length; i++) { var arr2 = arr1[i].split('='); if ( arr2[0] == key ) { return decodeURI(arr2[1]); } } } function removeCookie(key) { setCookie(key, '', -1); } </script> </head> <body> <input id="username" type="text"></input> <input type="button" id="btn1" value="登录"></input> <input type="button" id="btn2" value="删除"></input> </body> </html>