使用html css script 写一个登录页面
时间: 2023-12-29 22:02:19 浏览: 33
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
<style>
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
.container {
margin: 50px auto;
width: 400px;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
h2 {
text-align: center;
color: #333;
}
label {
display: block;
font-weight: bold;
margin-top: 20px;
}
input[type="text"], input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 5px;
margin-bottom: 20px;
}
button {
background-color: #4CAF50;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 20px;
width: 100%;
font-size: 16px;
font-weight: bold;
}
button:hover {
background-color: #3e8e41;
}
p {
text-align: center;
margin-top: 20px;
}
.error {
color: red;
font-weight: bold;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="container">
<h2>登录</h2>
<form>
<label for="username">用户名:</label>
<input type="text" name="username" id="username" required>
<label for="password">密码:</label>
<input type="password" name="password" id="password" required>
<button type="submit" name="submit">登录</button>
<p>还没有账号?<a href="#">注册</a></p>
<div id="error" class="error"></div>
</form>
</div>
<script>
// 获取表单和错误提示元素
const form = document.querySelector('form');
const error = document.querySelector('#error');
// 监听表单提交事件
form.addEventListener('submit', (event) => {
// 阻止默认提交行为
event.preventDefault();
// 获取用户名和密码输入框的值
const username = document.querySelector('#username').value;
const password = document.querySelector('#password').value;
// 判断用户名和密码是否为空
if (username === '' || password === '') {
error.textContent = '用户名和密码不能为空';
} else {
// TODO: 发送登录请求
error.textContent = '';
alert('登录成功!');
}
});
</script>
</body>
</html>
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)