如何用纸做一把枪 可发射子弹 如何用javascript做动态图
本文旨在解决使用 JavaScript 动态生成 HTML 按钮,并根据预定义的数据,将这些按钮按类别进行组织的问题。通过修改 HTML 元素属性以及使用模板字符串,可以有效地将按钮动态添加到页面,并实现点击按钮打开回复 URL的功能。同时,展示了如何将按钮放置到对应的类别下,提高代码的可维护性和可执行性。动态生成按钮
在前端开发中,经常需要根据数据动态生成HTML元素。下面示例展示了如何使用JavaScript一个包含按钮信息的队列中动态生成按钮,然后将其添加到指定的div元素中。
HTML结构:lt;h1gt;类别1lt;/h1gt;lt;h1gt;类别2lt;/h1gt;lt;div id=quot;buttonDivquot;gt;lt;/divgt;登录后复制
注意,这里将div的class属性修改为id属性,方便JavaScript代码可以通过 document.getElementById 方法准确地获取该元素。
立即学习“Java免费学习笔记(深入)”;
JavaScript代码:var buttonArr = [ { quot;categoryquot;:quot;Actionquot;,quot;namequot;:quot;Temple Run 2quot;,quot;urlquot;:quot;https://bigfoot9999.github.io/html5-games/games/templerun2/quot;}, { quot;categoryquot;:quot;Actionquot;,quot;namequot;:quot;斜坡游戏quot;,quot;urlquot;:quot;https://bigfoot9999.github.io/Slope-Game/quot;}];buttonArr.forEach(function (arrayItem) { console.log(arrayItem.name); console.log(arrayItem.url); document.getElementById('buttonDiv').innerHTML = `lt;按钮onClick='openGame(quot;${arrayItem.url}quot;)'gt;${arrayItem.name}lt;/buttongt;`;});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);}登录后复制
代码解释:buttonArr是一个包含按钮信息的队列,每个对象包含category(类别)、name(按钮名称)和 url(链接每个地址)。forEach 方法遍历 buttonArr 队列,对元素执行回调函数。document.getElementById('buttonDiv') 获取 id 为 buttonDiv 的 HTML 元素。.innerHTML = 将新的 HTML 字符串追加到该元素的内容中。使用了模板字符串(Template Literals) `...${expression}...` 来动态生成按钮的 HTML 代码。
${arrayItem.name} 将按钮标题插入到按钮的文本中, ${arrayItem.url} 将按钮链接插入到 onClick 事件处理函数中。注意,这里需要用双引号包裹 arrayItem.url,否则在HTML中会解析错误。openGame(url) 函数用于在新闻或标签页中打开指定的 URL。如果窗口已经,将其置于前台。
注意事项: Devin
世界上头脑AI软件工程师,可以独立完成各种开发任务。156查看详情保证HTML元素存在并且id属性与JavaScript代码中使用的getElementById方法的参数匹配。使用模板字符串可以更方便地插入变量和表达式,避免字符串拼接错误。openGame函数可以根据实际需求进行修改,例如,使用window.location.href在当前窗口中打开链接。按类别组织按钮
如果需要将按钮按类别进行组织,可以先创建一个对象,以类别作为键,按钮队列作为值。然后,根据这个对象动态生成HTML元素。
修改后的 JavaScript 代码:var buttonArr = [ { quot;categoryquot;:quot;Actionquot;,quot;namequot;:quot;Temple Run 2quot;,quot;urlquot;:quot;https://bigfoot9999.github.io/html5-games/games/templerun2/quot;}, { quot;categoryquot;:quot;Actionquot;,quot;namequot;:quot;Slope Gamequot;,quot;urlquot;:quot;https://bigfoot9999.github.io/Slope-Game/quot;}, { quot;categoryquot;:quot;Puzzlequot;,quot;namequot;:quot;2048quot;,quot;urlquot;:quot;https://bigfoot9999.github.io/html5-games/games/2048/quot;}];// 创建一个以类别为按钮,按钮队列为值的对象 constcategorizedButtons = {};buttonArr.forEach(item =gt; { if (!categorizedButtons[item.category]) {categorizedButtons[item.category] = []; }categorizedButtons[item.category].push(item);});//动态生成 HTML 元素 const buttonDiv = document.getElementById('buttonDiv');for (constcategory incategorizedButtons) { if (categorizedButtons.hasOwnProperty(category)) { constcategoryButtons =categorizedButtons[category]; //类别 constcategoryTitle = document.createElement('h2');categoryTitle.textContent =category;buttonDiv.appendChild(categoryTitle); //创建标题按钮列表常量按钮列表 = document.createElement('ul'); categoryButtons.forEach(button =gt; { const listItem = document.createElement('li'); const buttonElement = document.createElement('button'); buttonElement.t
extContent = 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);}登录后复制
代码解释:categorizedButtons对象用于存储按类别组织的按钮。通过buttonArr队列,将按钮添加到对应的类别下。然后categorizedButtons对象,为每个类别动态生成标题和按钮列表。使用document.createElement创建HTML元素,并设置其属性和内容。使用appendChild方法将元素添加到DOM树中。
总结:
通过以上方法,可以有效地使用JavaScript动态生成HTML按钮,并按类别进行组织。这种方法可以代码的可维护性和提高根据性,并且可以方便地数据动态更新页面。在实际开发中,可以根据具体需求对代码进行修改和优化。例如,使用CSS样式来美化按钮和列表的显示效果。
以上就是使用JavaScript动态生成HTML按钮并按类别组织相关内容,更多请关注乐哥详细常识网其他文章! 相关标签: css javascript java html 前端 git go html5 github app 前端开发 win JavaScript css html foreach 回调函数 字符串 class 对象 事件 dom location href insideHTML