用html帮我写一个具有登录注册功能且能够正常切换两个界面的页面
时间: 2023-11-28 18:04:26 浏览: 27
好的,以下是一个简单的 HTML 登录注册页面示例代码,包括切换两个界面的功能:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login & Register Page</title>
<style>
/* 登录和注册表单的样式 */
form {
margin: 20px auto;
padding: 20px;
width: 400px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f5f5f5;
}
input[type="text"], input[type="password"] {
display: block;
margin: 10px 0;
padding: 5px;
width: 100%;
border: 1px solid #ccc;
border-radius: 3px;
box-sizing: border-box;
font-size: 16px;
}
input[type="submit"] {
display: block;
margin: 20px auto;
padding: 10px 20px;
border: none;
border-radius: 3px;
background-color: #2196f3;
color: #fff;
font-size: 16px;
cursor: pointer;
}
/* 切换登录和注册表单的链接样式 */
.switch-link {
display: block;
margin: 20px auto;
text-align: center;
color: #2196f3;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<!-- 登录表单 -->
<form id="login-form">
<h2>Login</h2>
<label for="login-username">Username:</label>
<input type="text" id="login-username" name="username" required>
<label for="login-password">Password:</label>
<input type="password" id="login-password" name="password" required>
<input type="submit" value="Login">
<p class="switch-link" onclick="showRegisterForm()">Don't have an account? Register here.</p>
</form>
<!-- 注册表单 -->
<form id="register-form" style="display: none;">
<h2>Register</h2>
<label for="register-username">Username:</label>
<input type="text" id="register-username" name="username" required>
<label for="register-password">Password:</label>
<input type="password" id="register-password" name="password" required>
<input type="submit" value="Register">
<p class="switch-link" onclick="showLoginForm()">Already have an account? Login here.</p>
</form>
<script>
// 切换到登录表单
function showLoginForm() {
document.getElementById("login-form").style.display = "block";
document.getElementById("register-form").style.display = "none";
}
// 切换到注册表单
function showRegisterForm() {
document.getElementById("login-form").style.display = "none";
document.getElementById("register-form").style.display = "block";
}
</script>
</body>
</html>
```
该代码包括两个表单,一个登录表单和一个注册表单,它们都包含用户名和密码输入框以及提交按钮。这些表单初始时都处于隐藏状态,当用户点击“Don't have an account? Register here.”链接时,注册表单才会显示出来;当用户点击“Already have an account? Login here.”链接时,登录表单才会显示出来。这里使用了 JavaScript 来实现表单的切换功能。