原生 JS 与 ECMA 高级语法打造可增删改状态的品牌列表
以下是一个使用原生 JavaScript 和 ECMAScript 高级语法实现的品牌列表的示例代码: ```html <!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> </head> <body> <h2>品牌列表</h2> <input type="text" id="brandName" placeholder="输入品牌名称"> <button id="addBtn">添加</button> <table border="1" id="brandTable"> <tr> <th>行号</th> <th>品牌名称</th> <th>状态</th> <th>创建时间</th> <th>操作</th> </tr> </table> <script> // 定义一个类来表示品牌 class Brand { constructor(name, status, createdAt) { this.name = name; this.status = status; this.createdAt = createdAt; } } // 初始化品牌列表 let brandList = []; // 获取元素 const brandNameInput = document.getElementById('brandName'); const addBtn = document.getElementById('addBtn'); const brandTable = document.getElementById('brandTable'); // 添加品牌的函数 addBtn.addEventListener('click', () => { const name = brandNameInput.value; const status = '已启用'; const createdAt = new Date().toISOString(); const brand = new Brand(name, status, createdAt); brandList.push(brand); renderTable(); brandNameInput.value = ''; }); // 渲染表格的函数 function renderTable() { brandTable.innerHTML = ''; for (let i = 0; i < brandList.length; i++) { const brand = brandList[i]; const row = brandTable.insertRow(); const cell1 = row.insertCell(0); cell1.innerHTML = i + 1; const cell2 = row.insertCell(1); cell2.innerHTML = brand.name; const cell3 = row.insertCell(2); const statusBtn = document.createElement('button'); statusBtn.innerHTML = brand.status; statusBtn.addEventListener('click', () => { if (brand.status === '已启用') { brand.status = '已禁用'; } else { brand.status = '已启用'; } renderTable(); }); cell3.appendChild(statusBtn); const cell4 = row.insertCell(3); cell4.innerHTML = brand.createdAt; const cell5 = row.insertCell(4); const deleteBtn = document.createElement('button'); deleteBtn.innerHTML = '删除'; deleteBtn.addEventListener('click', () => { if (confirm('确定要删除这一行吗?')) { brandList.splice(i, 1); renderTable(); } }); cell5.appendChild(deleteBtn); } } renderTable(); </script> </body> </html> ``` 在上述代码中,我们定义了一个`Brand`类来表示品牌对象,包含品牌名称、状态和创建时间。通过添加按钮添加品牌,点击状态按钮切换状态,点击删除按钮确认后删除行,并重新渲染表格。 ######[AI写代码神器 | 814点数解答 | 2024-11-18 17:17:39]
- "Debugging 'Cannot Read Properties of Null (reading \'userid\')' Error in Vue Components: Steps and Example"(讯飞星火 | 614点数解答 | 2024-11-28 14:31:57)267
- HTML+jQuery 实现列表元素悬停动画效果:代码详解与注意事项(字节豆包 | 475点数解答 | 2024-12-19 14:58:05)367
- HTML+jQuery:打造动态导航栏动画效果及注意事项(字节豆包 | 495点数解答 | 2024-12-19 14:58:06)316
- HTML+jQuery:打造带动态交互效果的商品展示页面(讯飞星火 | 497点数解答 | 2024-12-19 14:58:18)321
- HTML与jQuery结合:实现商品列表交互动画的首页代码解析(讯飞星火 | 497点数解答 | 2024-12-19 14:58:20)337
- Three.js 示例代码解析:优化场景渲染与注意要点(字节豆包 | 164点数解答 | 2025-02-27 23:30:20)257
- Three.js实现可360°观察正交长方体:代码分析、优化与注意要点(DeepSeek | 466点数解答 | 2025-03-01 11:35:41)247
- 原生 JS 与 ECMA 高级语法打造可增删改状态的品牌列表(字节豆包 | 814点数解答 | 2024-11-18 17:17:39)147
- 原生 JS 与 ECMAScript 高级语法:实现可增删改状态的品牌列表(字节豆包 | 1015点数解答 | 2024-11-18 17:45:51)153
- 原生 JS 实现品牌列表:添加、删除与动态行号功能全解析(字节豆包 | 849点数解答 | 2024-11-18 21:15:31)130
- JavaScript开发难题破解:模拟“原生”方法及Node.js中的相关应用(阿里通义 | 192点数解答 | 2023-11-09 01:16:49)310
- Python+Pygame:打造酷炫科幻风格生日快乐动画代码!(DeepSeek | 1528点数解答 | 2025-06-23 15:00:40)157