以下是关于使用 JavaScript 制作登录页面的基础概念和相关内容:
基础概念:
优势:
类型:
应用场景:
常见问题及解决方法:
以下是一个简单的登录页面示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录页面</title>
<style>
/* 简单的样式 */
.login-form {
display: flex;
flex-direction: column;
width: 300px;
}
.login-form input {
margin-bottom: 10px;
padding: 5px;
}
.login-form button {
padding: 10px;
}
</style>
</head>
<body>
<form class="login-form" id="loginForm">
<input type="text" id="username" placeholder="用户名" required>
<input type="password" id="password" placeholder="密码" required>
<button type="submit">登录</button>
</form>
<script>
const loginForm = document.getElementById('loginForm');
loginForm.addEventListener('submit', function (event) {
event.preventDefault(); // 阻止默认提交行为
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
if (username === '' || password === '') {
alert('用户名和密码不能为空');
} else {
// 这里可以向服务器发送登录请求进行验证
console.log('用户名:', username);
console.log('密码:', password);
// 假设登录成功
alert('登录成功');
}
});
</script>
</body>
</html>
在上述示例中,当用户提交表单时,会先进行简单的非空验证,如果通过验证,可以在后续添加向服务器发送请求进行真实验证的代码。
领取专属 10元无门槛券
手把手带您无忧上云