用bootstrap做一个登陆注册页面代码(要有中文注释) 1.注册和登录可以切换 2.注册成功会跳转到登陆 3.注册失败会有提示:用户名已经存在
时间: 2024-05-19 07:16:08 浏览: 129
<!DOCTYPE html>
<html lang="zh-CN">
<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>登陆注册页面</title>
<!-- 引入bootstrap样式 -->
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row justify-content-center mt-5">
<div class="col-md-6">
<!-- 登陆表单 -->
<form id="login-form">
<h2 class="text-center">登陆</h2>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="login-username" placeholder="请输入用户名">
<label for="login-username">用户名</label>
</div>
<div class="form-floating mb-3">
<input type="password" class="form-control" id="login-password" placeholder="请输入密码">
<label for="login-password">密码</label>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">登陆</button>
<a href="#" id="register-link">没有账号?去注册</a>
</div>
</form>
<!-- 注册表单 -->
<form id="register-form" style="display:none;">
<h2 class="text-center">注册</h2>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="register-username" placeholder="请输入用户名">
<label for="register-username">用户名</label>
</div>
<div class="form-floating mb-3">
<input type="password" class="form-control" id="register-password" placeholder="请输入密码">
<label for="register-password">密码</label>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">注册</button>
<a href="#" id="login-link">已有账号?去登陆</a>
</div>
</form>
</div>
</div>
</div>
<!-- 引入jQuery -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- 引入bootstrap.js -->
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.1/js/bootstrap.min.js"></script>
<script>
$(function() {
// 切换到注册表单
$('#register-link').on('click', function() {
$('#login-form').hide();
$('#register-form').show();
});
// 切换到登陆表单
$('#login-link').on('click', function() {
$('#register-form').hide();
$('#login-form').show();
});
// 注册表单提交
$('#register-form').on('submit', function(e) {
e.preventDefault();
var username = $.trim($('#register-username').val());
var password = $.trim($('#register-password').val());
// 判断用户名是否存在
if (localStorage.getItem(username)) {
alert('用户名已经存在');
return;
}
// 将用户名和密码存储到localStorage中
localStorage.setItem(username, password);
alert('注册成功,请登陆');
$('#register-form').hide();
$('#login-form').show();
});
// 登陆表单提交
$('#login-form').on('submit', function(e) {
e.preventDefault();
var username = $.trim($('#login-username').val());
var password = $.trim($('#login-password').val());
// 判断用户名和密码是否正确
if (localStorage.getItem(username) === password) {
alert('登陆成功');
// TODO: 跳转到个人主页
} else {
alert('用户名或密码错误');
}
});
});
</script>
</body>
</html>
阅读全文