本文旨在解决使用 JavaScript动态生成 HTML 按钮,并根据预定义的数据,将这些按钮按类别进行组织的问题。通过修改 HTML 元素属性以及使用模板字符串,可以有效地将按钮动态添加到页面,并实现点击按钮打开对应 URL 的功能。同时,展示了如何将按钮放置到对应的类别下,提高代码的可维护性和可读性。
在前端开发中,经常需要根据数据动态生成 HTML 元素。以下示例展示了如何使用 JavaScript 从一个包含按钮信息的数组中动态生成按钮,并将其添加到指定的 div 元素中。
HTML 结构:
<h1>Category 1</h1> <h1>Category 2</h1> <div id="buttonDiv"></div>
注意,这里将 div 的 class 属性修改为 id 属性,以便 JavaScript 代码可以通过 document.getElementById 方法准确地获取该元素。
立即学习“Java免费学习笔记(深入)”;
JavaScript 代码:
var buttonArr = [ { "category":"Action","name":"Temple Run 2","url":"https://bigfoot9999.github.io/html5-games/games/templerun2/"}, { "category":"Action","name":"Slope Game","url":"https://bigfoot9999.github.io/Slope-Game/"} ]; buttonArr.forEach(function (arrayItem) { console.log(arrayItem.name); console.log(arrayItem.url); document.getElementById('buttonDiv').innerHTML += `<button onClick='openGame("${arrayItem.url}")'>${arrayItem.name}</button>`; }); let win; function openGame(url) { if (win) { win.focus(); return; } win = window.open(); win.document.body.style.margin = '0'; win.document.body.style.height = '100vh'; const iframe = win.document.createElement('iframe'); iframe.style.border = 'none'; iframe.style.width = '100%'; iframe.style.height = '100%'; iframe.style.margin = '0'; iframe.src = url; win.document.body.appendChild(iframe); }
代码解释:
注意事项:
如果需要将按钮按类别进行组织,可以先创建一个对象,以类别作为键,按钮数组作为值。然后,根据这个对象动态生成 HTML 元素。
修改后的 JavaScript 代码:
var buttonArr = [ { "category":"Action","name":"Temple Run 2","url":"https://bigfoot9999.github.io/html5-games/games/templerun2/"}, { "category":"Action","name":"Slope Game","url":"https://bigfoot9999.github.io/Slope-Game/"}, { "category":"Puzzle","name":"2048","url":"https://bigfoot9999.github.io/html5-games/games/2048/"} ]; // 创建一个以类别为键,按钮数组为值的对象 const categorizedButtons = {}; buttonArr.forEach(item => { if (!categorizedButtons[item.category]) { categorizedButtons[item.category] = []; } categorizedButtons[item.category].push(item); }); // 动态生成 HTML 元素 const buttonDiv = document.getElementById('buttonDiv'); for (const category in categorizedButtons) { if (categorizedButtons.hasOwnProperty(category)) { const categoryButtons = categorizedButtons[category]; // 创建类别标题 const categoryTitle = document.createElement('h2'); categoryTitle.textContent = category; buttonDiv.appendChild(categoryTitle); // 创建按钮列表 const buttonList = document.createElement('ul'); categoryButtons.forEach(button => { const listItem = document.createElement('li'); const buttonElement = document.createElement('button'); buttonElement.textContent = button.name; buttonElement.onclick = function() { openGame(button.url); }; listItem.appendChild(buttonElement); buttonList.appendChild(listItem); }); buttonDiv.appendChild(buttonList); } } let win; function openGame(url) { if (win) { win.focus(); return; } win = window.open(); win.document.body.style.margin = '0'; win.document.body.style.height = '100vh'; const iframe = win.document.createElement('iframe'); iframe.style.border = 'none'; iframe.style.width = '100%'; iframe.style.height = '100%'; iframe.style.margin = '0'; iframe.src = url; win.document.body.appendChild(iframe); }
代码解释:
总结:
通过以上方法,可以有效地使用 JavaScript 动态生成 HTML 按钮,并按类别进行组织。这种方法可以提高代码的可维护性和可读性,并且可以方便地根据数据动态更新页面内容。在实际开发中,可以根据具体需求对代码进行修改和优化。 例如,可以使用 CSS 样式来美化按钮和列表的显示效果。
以上就是使用 JavaScript 动态生成 HTML 按钮并按类别组织的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号