
本教程详细介绍了如何使用JavaScript构建一个简单的图书管理系统,通过面向对象的方式定义图书,并将新增的图书对象动态渲染到HTML表格中。文章涵盖了数据模型定义、数据存储、DOM操作以及事件监听,旨在帮助开发者理解如何高效地管理前端数据并实时更新用户界面。
在现代Web应用开发中,动态地展示和管理数据是常见的需求。本教程将以一个“我的图书馆”项目为例,演示如何利用JavaScript的面向对象特性来创建数据模型,并通过DOM操作将这些数据实时呈现在HTML表格中。我们将学习如何定义图书对象、存储这些对象,并响应用户输入来更新表格内容。
首先,我们需要一个蓝图来表示每一本书。在JavaScript中,这可以通过构造函数来实现。Book 构造函数将用于创建具有 title(书名)、author(作者)和 pages(页数)属性的图书实例。
function Book(title, author, pages) {
this.title = title;
this.author = author;
this.pages = pages;
}为了管理多本图书,我们需要一个地方来存储这些 Book 实例。一个简单的数组 (bookArray) 是一个理想的选择。
立即学习“Java免费学习笔记(深入)”;
const bookArray = [];
addBookToLibrary 函数负责创建新的 Book 实例并将其添加到 bookArray 中。
function addBookToLibrary(title, author, pages) {
const book = new Book(title, author, pages);
bookArray.push(book);
return book; // 返回新创建的图书对象
}为了用户能够输入图书信息并查看列表,我们需要相应的HTML结构。
一个简单的表单,包含书名、作者和页数的输入字段,以及一个提交按钮。
<form action="#" method="post">
<label for="title">Title:</label>
<input type="text" id="title" name="title"><br>
<label for="author">Author:</label>
<input type="text" id="author" name="author"><br>
<label for="pages">Pages:</label>
<input type="text" id="pages" name="pages"><br>
<input type="submit" id="submit" value="Submit">
</form>一个用于展示图书信息的表格。为了方便JavaScript动态更新,我们为表格的 <tbody> 元素添加一个 id。
<table>
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Pages</th>
</tr>
</thead>
<tbody id="bookTableBody">
<!-- 动态生成的图书行将插入到这里 -->
</tbody>
</table>注意事项: 在原始问题中,HTML结构可能存在将<tr>元素直接添加到<p>标签或表格外的非<tbody>区域的问题。为了确保HTML的语义正确性和良好的浏览器兼容性,我们强烈建议将动态生成的表格行(<tr>)插入到<table>内的<tbody>元素中。上述HTML结构已对此进行了修正。
updateTable 函数是实现动态渲染的核心。它负责清空现有表格内容,然后遍历 bookArray 中的所有图书,为每本书创建一行并将其添加到表格中。
function updateTable() {
// 获取表格的 tbody 元素
const tableBody = document.getElementById("bookTableBody");
// 清空现有表格内容,防止重复添加
tableBody.innerHTML = "";
// 遍历 bookArray 中的每一本书
for (const book of bookArray) {
// 创建一个新的表格行
const row = document.createElement("tr");
// 使用模板字符串填充行内容
row.innerHTML = `
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.pages}</td>
`;
// 将新行添加到表格的 tbody 中
tableBody.appendChild(row);
}
}当用户点击“Submit”按钮时,我们需要捕获表单数据,创建新的图书对象,并更新表格。
document.getElementById("submit").addEventListener("click", function(event) {
// 阻止表单默认提交行为,避免页面刷新
event.preventDefault();
// 获取表单输入的值
const title = document.getElementById("title").value;
const author = document.getElementById("author").value;
const pages = document.getElementById("pages").value;
// 校验输入(可选,但推荐)
if (!title || !author || !pages) {
alert("请填写所有字段!");
return;
}
// 添加新书到数组
addBookToLibrary(title, author, pages);
// 更新表格显示
updateTable();
// 清空表单字段,方便下次输入
document.getElementById("title").value = "";
document.getElementById("author").value = "";
document.getElementById("pages").value = "";
});将以上所有部分组合起来,构成一个完整的HTML文件和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>My Library</title>
<style>
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
form label {
display: inline-block;
width: 60px;
margin-bottom: 5px;
}
form input[type="text"] {
margin-bottom: 10px;
width: 200px;
padding: 5px;
}
form input[type="submit"] {
padding: 8px 15px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
form input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>My Library Book List</h1>
<p>这是一个使用面向对象JavaScript构建的简单图书管理项目。</p>
<br>
<form action="#" method="post">
<label for="title">Title:</label>
<input type="text" id="title" name="title"><br>
<label for="author">Author:</label>
<input type="text" id="author" name="author"><br>
<label for="pages">Pages:</label>
<input type="text" id="pages" name="pages"><br>
<input type="submit" id="submit" value="Submit">
</form>
<br>
<table>
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Pages</th>
</tr>
</thead>
<tbody id="bookTableBody">
<!-- 动态生成的图书行将插入到这里 -->
</tbody>
</table>
<script src="script.js"></script>
</body>
</html>// 存储图书对象的数组
const bookArray = [];
// Book 构造函数,用于创建图书对象
function Book(title, author, pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
// 将新书添加到 bookArray 中
function addBookToLibrary(title, author, pages) {
const book = new Book(title, author, pages);
bookArray.push(book);
return book;
}
// 更新 HTML 表格以显示 bookArray 中的所有图书
function updateTable() {
const tableBody = document.getElementById("bookTableBody");
tableBody.innerHTML = ""; // 清空现有内容
for (const book of bookArray) {
const row = document.createElement("tr");
row.innerHTML = `
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.pages}</td>
`;
tableBody.appendChild(row);
}
}
// 监听表单提交事件
document.getElementById("submit").addEventListener("click", function(event) {
event.preventDefault(); // 阻止默认的表单提交行为
const title = document.getElementById("title").value;
const author = document.getElementById("author").value;
const pages = document.getElementById("pages").value;
if (!title || !author || !pages) {
alert("请填写所有字段!");
return;
}
addBookToLibrary(title, author, pages); // 添加新书
updateTable(); // 更新表格显示
// 清空表单字段
document.getElementById("title").value = "";
document.getElementById("author").value = "";
document.getElementById("pages").value = "";
});
// 页面加载时初始化表格(如果 bookArray 中有预设数据)
// 例如,可以添加一些初始数据:
// addBookToLibrary("The Hobbit", "J.R.R. Tolkien", 310);
// addBookToLibrary("1984", "George Orwell", 328);
updateTable(); // 首次加载时调用,确保表格显示最新数据通过本教程,我们学习了如何使用JavaScript有效地管理数据和更新UI。以下是一些关键的总结和最佳实践:
掌握这些技术,您将能够构建更具交互性和动态性的Web应用程序。
以上就是使用JavaScript动态管理和渲染对象到HTML表格的教程的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号