其他分享
首页 > 其他分享> > 记住用户名

记住用户名

作者:互联网

需要用到本地存储localStorage

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

 

标签:username,用户名,checked,remember,localStorage,getItem,var,记住
来源: https://www.cnblogs.com/meng-li/p/15944059.html