给我一份用html,css,js,写成的web前端代码
时间: 2024-02-29 10:51:45 浏览: 17
当然,以下是一个简单的例子,实现了一个简单的登录页面:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form>
<h2>登录</h2>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<button type="submit">登录</button>
</form>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.2);
margin: 50px auto;
max-width: 500px;
}
h2 {
margin-top: 0;
}
label {
display: block;
margin-bottom: 5px;
}
input[type=text], input[type=password] {
padding: 5px;
border: 1px solid #ccc;
border-radius: 3px;
width: 100%;
margin-bottom: 10px;
}
button[type=submit] {
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
border-radius: 3px;
cursor: pointer;
width: 100%;
}
button[type=submit]:hover {
background-color: #45a049;
}
```
JavaScript代码:
```javascript
const form = document.querySelector('form');
form.addEventListener('submit', e => {
e.preventDefault(); // 防止页面刷新
const username = form.querySelector('#username').value;
const password = form.querySelector('#password').value;
// 这里可以添加验证逻辑
console.log(`用户名:${username}\n密码:${password}`);
});
```
这是一个非常简单的例子,实现了一个基本的登录页面,并且添加了一些简单的样式和表单验证逻辑。当用户提交表单时,会在控制台打印出用户名和密码。