第一个界面
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <input type="text" name="" id="userName" value="" /></br> <input type="text" name="" id="userPwd" value="" /></br> <button type="button" id="register">注册</button> <a href="2cookieLogin.html">去登陆</a> </body> </html> <script type="text/javascript"> let oName = document.getElementById("userName"); let oPwd = document.getElementById("userPwd"); let oRegister = document.getElementById("register"); //存 oRegister.onclick=function(){ document.cookie = "name="+oName.value; document.cookie = "pwd="+oPwd.value; } </script>第二个界面
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <input type="text" name="" id="userName" value="" /></br> <input type="text" name="" id="userPwd" value="" /></br> <button type="button" id="login">登陆</button> </body> </html> <script type="text/javascript"> let oUserName = document.getElementById("userName"); let oUserPwd = document.getElementById("userPwd"); let oLogin = document.getElementById("login"); oLogin.onclick=function(){ let strcookie = document.cookie; //cookie拆分成数组 let arrcookie = strcookie.split("; "); let cookieName=null; let cookiePwd=null; for(let i=0; i<arrcookie.length; i++){ let items = arrcookie[i].split("="); if(items[0] == "name"){ cookName = items[1]; }else if(items[0] == "pwd"){ cookPwd = items[1]; } } if(oUserName.value == cookName && oUserPwd.value == cookPwd){ console.log("登录成功"); }else{ console.log("登录失败"); } } </script>