原生 JS 与 ECMAScript 高级语法打造可增删的品牌列表
以下是一个使用原生 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> <table id="brandTable"> <thead> <tr> <th>#</th> <th>品牌名称</th> <th>状态</th> <th>创建时间</th> <th>操作</th> </tr> </thead> <tbody> </tbody> </table> <button id="addBtn">添加</button> <script> // 获取表格和添加按钮 const table = document.getElementById('brandTable').getElementsByTagName('tbody')[0]; const addBtn = document.getElementById('addBtn'); // 初始化行号 let rowNumber = 1; // 添加行函数 addBtn.addEventListener('click', () => { const newRow = table.insertRow(); const cell1 = newRow.insertCell(0); const cell2 = newRow.insertCell(1); const cell3 = newRow.insertCell(2); const cell4 = newRow.insertCell(3); const cell5 = newRow.insertCell(4); cell1.innerHTML = rowNumber; cell2.innerHTML = `<input type="text" />`; cell3.innerHTML = `<select><option value="active">活跃</option><option value="inactive">不活跃</option></select>`; cell4.innerHTML = new Date().toISOString(); cell5.innerHTML = `<button class="deleteBtn">删除</button>`; rowNumber++; }); // 为删除按钮添加点击事件 table.addEventListener('click', (event) => { if (event.target.classList.contains('deleteBtn')) { if (confirm('确定要删除这一行吗?')) { const row = event.target.parentNode.parentNode; table.deleteRow(row.rowIndex); } } }); </script> </body> </html> ``` 在上述代码中,实现了点击添加按钮添加新行,点击删除按钮出现确认框并根据确认结果删除行的功能。创建时间使用了`new Date().toISOString()`获取当前时间。 ######[AI写代码神器 | 602点数解答 | 2024-11-18 18:03:43]
- "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
- JavaScript开发难题破解:模拟“原生”方法及Node.js中的相关应用(阿里通义 | 192点数解答 | 2023-11-09 01:16:49)310
- Python+Pygame:打造酷炫科幻风格生日快乐动画代码!(DeepSeek | 1528点数解答 | 2025-06-23 15:00:40)156
- 超酷炫!用HTML打造科幻风生日快乐祝福网页(DeepSeek | 2235点数解答 | 2025-06-23 15:03:49)179
- "Debugging 'Cannot Read Properties of Null (Reading 'userid')' Error: A Step-by-Step Guide"(讯飞星火 | 523点数解答 | 2024-11-28 14:32:05)251
- 解决 “Cannot read properties of null (reading 'userid')” 错误的代码修改方案(讯飞星火 | 516点数解答 | 2024-11-28 14:32:43)283