把刚刚这段代码用css进行交互,以完成完整的登陆页面功能
时间: 2024-02-11 11:04:49 浏览: 34
好的,以下是使用 CSS 和一些 JavaScript 交互来完善上面的 HTML 登录页面的代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
max-width: 500px;
margin: 50px auto;
}
h1 {
text-align: center;
}
label {
display: block;
margin-bottom: 10px;
}
input[type="text"],
input[type="password"] {
display: block;
width: 100%;
padding: 10px;
border-radius: 5px;
border: none;
margin-bottom: 20px;
box-sizing: border-box;
}
input[type="submit"] {
background-color: #4CAF50;
color: #fff;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
.error {
color: red;
font-size: 14px;
margin-top: 5px;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<form id="login-form" action="#" method="post">
<h1>Login</h1>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Log In">
<p id="error-message" class="error hidden">Incorrect username or password.</p>
</form>
<script>
const form = document.getElementById('login-form');
const errorMessage = document.getElementById('error-message');
form.addEventListener('submit', event => {
// Prevent the form from submitting
event.preventDefault();
// Get the username and password values
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
// Check if the username and password are correct
if (username === 'admin' && password === 'password') {
// Redirect to the home page (replace "#" with the actual URL)
window.location.href = '#';
} else {
// Show an error message
errorMessage.classList.remove('hidden');
}
});
</script>
</body>
</html>
```
这段代码增加了一个 ID 为 `login-form` 的表单元素以及一个 ID 为 `error-message` 的错误消息元素。它还添加了一些 CSS 样式来隐藏错误消息,以便在需要时显示出来。
使用 JavaScript,我们将表单的 `submit` 事件监听器添加到 `form` 元素上。当表单提交时,它将阻止默认行为(即发送 POST 请求),获取用户名和密码字段的值,检查它们是否正确,如果正确,则将用户重定向到主页(使用 `window.location.href`),否则显示错误消息。
希望这能帮助到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)