登录界面的代码html
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.container h2 {
text-align: center;
}
.container form {
margin-top: 20px;
}
.container form label {
display: block;
margin-bottom: 5px;
}
.container form input[type="text"],
.container form input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
.container form button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
.container form button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h2>Login</h2>
<form action="#" method="POST">
<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>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
这段代码创建了一个简单的登录界面,其中包含用户名和密码的输入字段以及登录按钮。CSS样式用于美化界面,使其看起来更加吸引人。您可以根据自己的需要对其进行定制和扩展。
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.container h2 {
text-align: center;
}
.container form {
margin-top: 20px;
}
.container form label {
display: block;
margin-bottom: 5px;
}
.container form input[type="text"],
.container form input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
.container form button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
.container form button:hover {
background-color: #0056b3;
}
.error-message {
color: red;
margin-top: 5px;
}
</style>
</head>
<body>
<div class="container">
<h2>Login</h2>
<form id="loginForm" action="#" method="POST" onsubmit="return validateForm()">
<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>
<button type="submit">Login</button>
<p class="error-message" id="errorMessage"></p>
</form>
</div>
<script>
function validateForm() {
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
var errorMessage = document.getElementById('errorMessage');
if (username === '' || password === '') {
errorMessage.textContent = 'Please enter both username and password.';
return false;
}
// You can add more complex validation logic here if needed
return true;
}
</script>
</body>
</html>
在这个更新的代码中,我们添加了一个用于显示错误消息的段落元素,并在JavaScript中添加了一个简单的表单验证函数。当用户尝试提交表单时,该函数将检查用户名和密码是否为空,如果为空,则显示错误消息并阻止表单提交。