html注册与登录网页制作
时间: 2023-11-16 16:04:14 浏览: 37
制作一个基本的 HTML 注册与登录网页,可以分为以下几个步骤:
1. 创建 HTML 文件,并引入必要的 CSS 和 JavaScript 文件。
```
<!DOCTYPE html>
<html>
<head>
<title>注册与登录网页</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
</body>
</html>
```
2. 添加注册表单和登录表单的 HTML 代码。
```
<body>
<div class="container">
<form id="register-form" class="form" action="#" method="post">
<h2>注册</h2>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<label for="confirm-password">确认密码:</label>
<input type="password" id="confirm-password" name="confirm-password" required>
<button type="submit" class="btn">注册</button>
</form>
<form id="login-form" class="form" action="#" method="post">
<h2>登录</h2>
<label for="login-username">用户名:</label>
<input type="text" id="login-username" name="login-username" required>
<label for="login-password">密码:</label>
<input type="password" id="login-password" name="login-password" required>
<button type="submit" class="btn">登录</button>
</form>
</div>
</body>
```
3. 添加 CSS 样式。
```
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
max-width: 500px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.2);
}
.form {
display: none;
}
.form h2 {
text-align: center;
margin-bottom: 20px;
}
.form label {
display: block;
margin-bottom: 10px;
}
.form input {
display: block;
width: 100%;
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
border: none;
background-color: #f4f4f4;
}
.form button {
display: block;
width: 100%;
padding: 10px;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.form button:hover {
background-color: #3e8e41;
}
.form a {
display: block;
text-align: center;
margin-top: 20px;
}
```
4. 添加 JavaScript 脚本,用于切换注册和登录表单。
```
const registerForm = document.getElementById("register-form");
const loginForm = document.getElementById("login-form");
function showRegisterForm() {
registerForm.style.display = "block";
loginForm.style.display = "none";
}
function showLoginForm() {
registerForm.style.display = "none";
loginForm.style.display = "block";
}
showLoginForm(); // 默认显示登录表单
document.getElementById("show-register-form").addEventListener("click", showRegisterForm);
document.getElementById("show-login-form").addEventListener("click", showLoginForm);
```
5. 最后,保存文件并在浏览器中打开以查看效果。
完整的 HTML、CSS 和 JavaScript 代码如下:
HTML:
```
<!DOCTYPE html>
<html>
<head>
<title>注册与登录网页</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="container">
<form id="register-form" class="form" action="#" method="post">
<h2>注册</h2>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<label for="confirm-password">确认密码:</label>
<input type="password" id="confirm-password" name="confirm-password" required>
<button type="submit" class="btn">注册</button>
<a href="#" id="show-login-form">已有账号?点击登录</a>
</form>
<form id="login-form" class="form" action="#" method="post">
<h2>登录</h2>
<label for="login-username">用户名:</label>
<input type="text" id="login-username" name="login-username" required>
<label for="login-password">密码:</label>
<input type="password" id="login-password" name="login-password" required>
<button type="submit" class="btn">登录</button>
<a href="#" id="show-register-form">还没有账号?点击注册</a>
</form>
</div>
</body>
</html>
```
CSS:
```
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
max-width: 500px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.2);
}
.form {
display: none;
}
.form h2 {
text-align: center;
margin-bottom: 20px;
}
.form label {
display: block;
margin-bottom: 10px;
}
.form input {
display: block;
width: 100%;
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
border: none;
background-color: #f4f4f4;
}
.form button {
display: block;
width: 100%;
padding: 10px;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.form button:hover {
background-color: #3e8e41;
}
.form a {
display: block;
text-align: center;
margin-top: 20px;
}
```
JavaScript:
```
const registerForm = document.getElementById("register-form");
const loginForm = document.getElementById("login-form");
function showRegisterForm() {
registerForm.style.display = "block";
loginForm.style.display = "none";
}
function showLoginForm() {
registerForm.style.display = "none";
loginForm.style.display = "block";
}
showLoginForm(); // 默认显示登录表单
document.getElementById("show-register-form").addEventListener("click", showRegisterForm);
document.getElementById("show-login-form").addEventListener("click", showLoginForm);
```