html+css+js做一个登录注册页面用cookie存储
时间: 2024-05-16 14:16:41 浏览: 77
好的,下面是一个简单的示例代码:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login and Registration Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Login and Registration Page</h1>
<div class="login">
<h2>Login</h2>
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<input type="checkbox" id="remember" name="remember">
<label for="remember">Remember me</label><br>
<input type="submit" value="Login">
</form>
</div>
<div class="register">
<h2>Registration</h2>
<form action="#" method="post">
<label for="new-username">Username:</label>
<input type="text" id="new-username" name="new-username" required><br>
<label for="new-password">Password:</label>
<input type="password" id="new-password" name="new-password" required><br>
<label for="confirm-password">Confirm Password:</label>
<input type="password" id="confirm-password" name="confirm-password" required><br>
<input type="submit" value="Register">
</form>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f2f2f2;
}
h1 {
text-align: center;
padding: 20px;
background-color: #333;
color: #fff;
margin: 0;
}
.login, .register {
margin: 20px;
padding: 20px;
background-color: #fff;
border: 1px solid #ccc;
box-shadow: 0 0 10px #ccc;
}
.login {
float: left;
width: 40%;
}
.register {
float: right;
width: 40%;
}
input[type="text"], input[type="password"], input[type="submit"], input[type="checkbox"] {
padding: 10px;
margin-bottom: 10px;
display: block;
width: 100%;
box-sizing: border-box;
}
label {
font-weight: bold;
}
input[type="submit"] {
background-color: #333;
color: #fff;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #444;
}
.error {
color: red;
font-weight: bold;
margin-top: 10px;
}
```
JavaScript代码:
```
// Check for saved username and password
window.onload = function() {
var remember = document.getElementById("remember");
if (getCookie("username") != "" && getCookie("password") != "") {
document.getElementById("username").value = getCookie("username");
document.getElementById("password").value = getCookie("password");
remember.checked = true;
}
}
// Save username and password if "Remember me" is checked
document.querySelector("form").addEventListener("submit", function(e) {
e.preventDefault();
var remember = document.getElementById("remember");
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (remember.checked) {
setCookie("username", username, 30);
setCookie("password", password, 30);
} else {
setCookie("username", "", -1);
setCookie("password", "", -1);
}
alert("Login successful!");
});
// Check if password and confirm password match when registering
document.querySelector(".register form").addEventListener("submit", function(e) {
var password = document.getElementById("new-password").value;
var confirm_password = document.getElementById("confirm-password").value;
if (password != confirm_password) {
e.preventDefault();
document.querySelector(".register .error").innerHTML = "Passwords do not match!";
} else {
document.querySelector(".register .error").innerHTML = "";
}
});
// Functions for setting and getting cookies
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return "";
}
```
在这个示例中,使用了JavaScript的cookie存储功能,当用户勾选“Remember me”时,会在cookie中保存用户名和密码。当用户再次访问登录页面时,如果cookie中有保存的用户名和密码,页面会自动填充这些信息。同时,当用户进行登录操作时,JavaScript会检查是否勾选了“Remember me”,如果勾选了则会将用户名和密码保存到cookie中。在注册表单中,JavaScript还会检查密码和确认密码是否一致,如果不一致,则不会提交表单。
阅读全文