下面我们将使用 JavaScript、HTML 和 CSS 构建一个简单的网页,其中包含动态用户信息展示和交互功能。用户可以输入名字和年龄,点击按钮后,页面将动态显示用户信息。
1. 项目需求
- 用户输入: 用户可以通过输入框填写自己的名字和年龄。
- 展示信息: 点击提交按钮后,页面将显示用户的姓名和年龄,并且可以动态更新。
- 基本样式: 页面将具有简洁的样式,输入框和按钮可交互。
2. 结构设计
- 使用 HTML 来构建网页结构。
- 使用 CSS 来设置基本样式,使页面看起来整洁。
- 使用 JavaScript 来处理用户交互并动态更新页面。
3. 代码实现
HTML 代码(index.html
)
html
复制代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dynamic User Info</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="container"> <h1>Enter Your Information</h1> <form id="userForm"> <label for="name">Name:</label> <input type="text" id="name" placeholder="Enter your name" required> <br> <label for="age">Age:</label> <input type="number" id="age" placeholder="Enter your age" required> <br> <button type="submit">Submit</button> </form> <div id="userInfo" class="hidden"> <h2>User Information</h2> <p id="displayName"></p> <p id="displayAge"></p> </div> </div> <script src="script.js"></script> </body> </html>
CSS 代码(styles.css
)
css
复制代码
/* General Styles */ body { font-family: Arial, sans-serif; background-color: #f4f7fc; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; } .container { background-color: #ffffff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); padding: 20px; width: 300px; text-align: center; } h1 { color: #333; } form { display: flex; flex-direction: column; margin-bottom: 20px; } label { text-align: left; margin-top: 10px; font-size: 14px; } input { padding: 8px; margin: 5px 0; border-radius: 4px; border: 1px solid #ccc; } button { padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; } button:hover { background-color: #45a049; } /* Hidden section for user info */ .hidden { display: none; } #userInfo { margin-top: 20px; background-color: #f9f9f9; padding: 10px; border-radius: 8px; border: 1px solid #ddd; }
JavaScript 代码(script.js
)
javascript
复制代码
// Get the form and the div that will show user information const userForm = document.getElementById('userForm'); const userInfo = document.getElementById('userInfo'); const displayName = document.getElementById('displayName'); const displayAge = document.getElementById('displayAge'); // Event listener for form submission userForm.addEventListener('submit', function(event) { event.preventDefault(); // Prevent form from refreshing the page // Get values from the input fields const name = document.getElementById('name').value; const age = document.getElementById('age').value; // Check if both fields are filled if (name && age) { // Display the user information displayName.textContent = `Name: ${name}`; displayAge.textContent = `Age: ${age}`; // Show the user information div userInfo.classList.remove('hidden'); } else { alert('Please fill out both fields!'); } });
4. 代码解析
-
HTML 部分:
- 页面包括了一个表单,用户可以输入姓名和年龄。
- 在用户提交信息后,会显示一个包含用户姓名和年龄的区域。
userInfo
区域使用了一个hidden
类来在提交前隐藏。
-
CSS 部分:
- 页面通过
flexbox
布局将内容居中。 - 表单、按钮和显示区域的样式被精心设计,确保页面看起来简洁且用户友好。
.hidden
类用于隐藏用户信息区域,直到用户提交表单后显示。
- 页面通过
-
JavaScript 部分:
- 在用户提交表单时,通过
addEventListener
捕捉到submit
事件。 - 使用
event.preventDefault()
防止表单默认的页面刷新行为。 - 获取用户输入的姓名和年龄,并进行验证,如果输入有效,则显示用户信息。
- 在用户提交表单时,通过
5. 演示效果
- 页面打开时,用户看到一个表单,要求输入姓名和年龄。
- 用户输入信息并点击提交按钮后,页面会显示用户输入的姓名和年龄。
- 如果输入框为空,点击提交按钮时会弹出提示“Please fill out both fields!”。
6. 进一步扩展
- 增加表单验证: 你可以扩展表单验证功能,确保输入符合预期,例如验证年龄是否为正数。
- 样式提升: 使用 CSS 动画或过渡效果,使得提交按钮点击后的反馈更加生动。
- 数据存储: 通过
localStorage
存储用户的输入信息,页面刷新后仍能显示之前的信息。 - 异步请求: 你可以通过
fetch
或axios
向服务器提交用户信息,实现更复杂的交互。
总结
通过这个案例,你可以了解如何使用 JavaScript、HTML 和 CSS 创建一个简单的动态网页应用。该应用通过表单输入和动态显示用户信息来展示 JavaScript 的交互能力。通过不断改进和扩展,可以将其发展为一个功能更加复杂的网页应用。
访问更多内容来源 https://ai.tmqcjr.com