运用HTML+CSS+JavaScript前端技术实现,写出一段用户登录界面代码,包括账号密码和验证码
时间: 2024-06-10 16:06:25 浏览: 173
html,css和js写的登录与注册界面
下面是一段实现用户登录界面的代码,包括账号密码和验证码:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>User Login</h1>
<form>
<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>
<label for="captcha">Captcha:</label>
<input type="text" id="captcha" name="captcha" required>
<img src="captcha.php" alt="Captcha" id="captcha_image">
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
```
CSS代码:
```
body {
background-color: #f1f1f1;
font-family: Arial, sans-serif;
}
.container {
background-color: #fff;
margin: 50px auto;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.3);
max-width: 500px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 10px;
}
input[type="text"], input[type="password"] {
padding: 10px;
border-radius: 5px;
border: none;
margin-bottom: 20px;
}
input[type="text"]#captcha {
width: 50%;
display: inline-block;
margin-right: 10px;
}
img#captcha_image {
display: inline-block;
vertical-align: middle;
margin-bottom: 10px;
}
button[type="submit"] {
padding: 10px;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button[type="submit"]:hover {
background-color: #3e8e41;
}
@media only screen and (max-width: 600px) {
input[type="text"]#captcha {
width: 100%;
margin-right: 0;
margin-bottom: 10px;
}
img#captcha_image {
display: block;
margin: 0 auto;
}
}
```
JavaScript代码:
```
// 获取验证码图片
var captcha_image = document.getElementById("captcha_image");
// 点击验证码图片刷新验证码
captcha_image.onclick = function() {
this.src = "captcha.php?" + Math.random();
}
```
其中,`captcha.php`文件是生成验证码图片的PHP脚本,不在此赘述。
阅读全文