<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title></title> </head> <body> <input type="text" id="uname"><input type="checkbox" name="" id="remember">记住用户名 <script> var uname = document.querySelector("#uname"); var remember = document.querySelector("#remember"); if (localStorage.getItem('uname')) { uname.value = localStorage.getItem('uname'); console.log(localStorage.getItem('uname')); remember.checked = true; } remember.addEventListener('change', function() { if (this.checked) { localStorage.setItem('uname', uname.value); } else { localStorage.removeItem('uname'); } }) uname.addEventListener('focus', function() { uname.value = localStorage.getItem('uname') }) </script> </body> </html>