利用格式化标签,实现登录页面的中英文网页的切换。html
时间: 2024-01-22 12:19:32 浏览: 115
以下是一个简单的登录页面,可以通过点击中英文切换按钮来切换页面语言:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<style>
body {
font-family: Arial, sans-serif;
background-color: #F5F5F5;
}
.container {
width: 30%;
margin: auto;
margin-top: 50px;
padding: 20px;
background-color: #FFF;
box-shadow: 0px 0px 10px rgba(0,0,0,0.3);
}
input[type=text], input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin-top: 20px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color: #45a049;
}
.switch {
display: inline-block;
position: absolute;
top: 10px;
right: 10px;
}
.switch input {
display: none;
}
.switch label {
cursor: pointer;
color: #888;
font-size: 14px;
font-weight: normal;
}
.switch label:first-of-type {
margin-right: 10px;
}
.switch label:hover {
color: #444;
}
.switch input:checked + label {
color: #000;
font-weight: bold;
}
h1 {
text-align: center;
margin-top: 0;
}
p {
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Login</h1>
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" placeholder="Enter your username">
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter your password">
<button type="submit">Login</button>
</form>
<div class="switch">
<input type="checkbox" id="language-switch">
<label for="language-switch">English</label>
<label for="language-switch">中文</label>
</div>
<p>© 2021 MyWebsite.com. All rights reserved.</p>
</div>
<script>
const languageSwitch = document.getElementById('language-switch');
const usernameLabel = document.querySelector('label[for="username"]');
const passwordLabel = document.querySelector('label[for="password"]');
const loginButton = document.querySelector('button[type="submit"]');
languageSwitch.addEventListener('change', () => {
if (languageSwitch.checked) {
// Switch to Chinese
document.documentElement.lang = 'zh-CN';
usernameLabel.textContent = '用户名:';
passwordLabel.textContent = '密码:';
loginButton.textContent = '登录';
} else {
// Switch to English
document.documentElement.lang = 'en-US';
usernameLabel.textContent = 'Username:';
passwordLabel.textContent = 'Password:';
loginButton.textContent = 'Login';
}
});
</script>
</body>
</html>
```
在上面的代码中,我们使用了 `label` 标签来为每个表单项添加标签,使得用户可以更加方便地输入信息。同时,我们也添加了一个切换按钮,使用 `input[type="checkbox"]` 和 `label` 标签来实现。当点击按钮时,我们使用 JavaScript 来更改页面语言,并更新对应的文本内容。
阅读全文