以下是一个简单的JavaScript登录注册功能的完整代码示例:
<!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>
<div id="loginForm">
<h2>登录</h2>
<input type="text" id="loginUsername" placeholder="用户名">
<input type="password" id="loginPassword" placeholder="密码">
<button onclick="login()">登录</button>
</div>
<div id="registerForm">
<h2>注册</h2>
<input type="text" id="registerUsername" placeholder="用户名">
<input type="password" id="registerPassword" placeholder="密码">
<button onclick="register()">注册</button>
</div>
<script src="script.js"></script>
</body>
</html>// 模拟的用户数据库
let users = {};
function register() {
const username = document.getElementById('registerUsername').value;
const password = document.getElementById('registerPassword').value;
if (users[username]) {
alert('用户名已存在');
return;
}
users[username] = password;
alert('注册成功');
document.getElementById('registerForm').style.display = 'none';
document.getElementById('loginForm').style.display = 'block';
}
function login() {
const username = document.getElementById('loginUsername').value;
const password = document.getElementById('loginPassword').value;
if (users[username] === password) {
alert('登录成功');
} else {
alert('用户名或密码错误');
}
}希望这个示例能帮助你理解基本的登录注册功能的实现。如果有更多具体问题,欢迎继续提问!
没有搜到相关的文章