用原生js写一个登录页面
时间: 2023-05-22 19:01:34 浏览: 70
好的,以下是一个简单的登录页面的原生JS代码:
```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.2);
max-width: 400px;
margin: 0 auto;
}
input[type="text"], input[type="password"] {
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
border: none;
box-shadow: 0px 0px 5px rgba(0,0,0,0.1);
width: 100%;
}
input[type="submit"] {
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 5px;
padding: 10px;
cursor: pointer;
width: 100%;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<form>
<h2>Login</h2>
<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="Login">
</form>
<script>
// 获取表单元素
var form = document.querySelector('form');
var usernameInput = document.querySelector('#username');
var passwordInput = document.querySelector('#password');
// 监听表单提交事件
form.addEventListener('submit', function(event) {
event.preventDefault(); // 阻止表单默认提交行为
// 获取输入的用户名和密码
var username = usernameInput.value;
var password = passwordInput.value;
// TODO: 在这里处理登录逻辑,比如发送 AJAX 请求到后端验证用户名和密码
// 登录成功后跳转到首页
window.location.href = 'index.html';
});
</script>
</body>
</html>
```
这是一个简单的登录页面,包含一个表单,用户需要输入用户名和密码才能登录。在表单提交时,我们可以获取输入的用户名和密码,然后发送 AJAX 请求到后端验证用户名和密码,如果验证通过,则跳转到首页。
相关推荐
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)