当前位置:网站首页>记住用户名案例(js)
记住用户名案例(js)
2022-08-03 11:03:00 【Cap07】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<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');
//先判断有没有输入用户名,如果输入,更新value值为用户名
if (localStorage.getItem('username')) {
username.value = localStorage.getItem('username');
remember.checked = true; //用于下次打开页面时,若用户名已记住,则此框默认勾选
}
//当框改变时,根据情况对localStorage进行相应改变
remember.addEventListener('change', function() {
if (this.checked) {
localStorage.setItem('username', username.value)
} else {
localStorage.removeItem('username');
}
})
</script>
</body>效果:
边栏推荐
猜你喜欢
随机推荐
MATLAB Programming and Applications 2.6 Strings
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
[Detailed explanation of binary search plus recursive writing method] with all the code
QT with OpenGL(Shadow Mapping)(面光源篇)
Machines need tokens more than people
完全背包问题的思路解析
【TypeScript】为什么要选择 TypeScript?
What is the relationship between The Matrix and 6G?
SAP 电商云 Spartacus UI 的 External Routes 设计明细
fast planner中拓扑路径搜索
嵌入式软件组件经典架构与存储器分类
For invoice processing DocuWare, cast off the yoke of the paper and data input, automatic processing all the invoice received
build --repot
图新地球为什么很模糊,白球、看图、下载问题深度剖析
【文件IO的简单实现】
numpy
请问应该用什么关键字将内容主题设置为 dark 呢
oracle计算同、环比
509. 斐波那契数
「全球数字经济大会」登陆 N 世界,融云提供通信云服务支持









