这段代码怎么实现登陆界面
时间: 2024-04-30 14:21:38 浏览: 10
要实现登录界面,需要使用一些前端技术,比如 HTML、CSS 和 JavaScript。以下是一个简单的示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style type="text/css">
body {
background-color: #f1f1f1;
font-family: Arial, sans-serif;
}
form {
background-color: #fff;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
padding: 20px;
width: 400px;
margin: 100px auto;
}
h1 {
font-size: 24px;
margin-top: 0;
}
label {
display: block;
margin-bottom: 10px;
}
input[type="text"],
input[type="password"] {
display: block;
padding: 10px;
width: 100%;
border-radius: 5px;
border: none;
margin-bottom: 20px;
}
input[type="submit"] {
background-color: #4CAF50;
border: none;
color: #fff;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
.error {
color: red;
margin-top: 10px;
}
</style>
</head>
<body>
<form id="login-form">
<h1>Login</h1>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<input type="submit" value="Login">
<div class="error" id="error-message"></div>
</form>
<script type="text/javascript">
const form = document.getElementById('login-form');
const errorMessage = document.getElementById('error-message');
form.addEventListener('submit', (event) => {
event.preventDefault();
const username = event.target.username.value;
const password = event.target.password.value;
if (username === 'admin' && password === 'password') {
window.location.href = 'dashboard.html';
} else {
errorMessage.innerHTML = 'Invalid username or password';
}
});
</script>
</body>
</html>
```
这段代码使用了 HTML、CSS 和 JavaScript 来创建一个简单的登录界面。当用户提交表单时,JavaScript 代码会验证用户名和密码是否正确。如果正确,页面会跳转到仪表板页面,否则会显示错误消息。