
本文档旨在解决如何使用 JavaScript 从数组数据动态生成 HTML 按钮,并根据其类别进行组织的问题。通过使用 forEach 循环遍历数据,并利用模板字符串动态创建按钮元素,最终将这些按钮添加到对应的类别容器中。同时,还提供了打开游戏链接的 openGame 函数,以便用户点击按钮后能够在新窗口中打开游戏。
要动态生成 HTML 按钮,首先需要一个包含按钮数据的数组。例如:
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/"}
];然后,可以使用 forEach 循环遍历该数组,并为每个数组项创建一个按钮元素。关键在于使用模板字符串(template literals)来动态生成按钮的 onClick 属性和按钮文本。
buttonArr.forEach(function (arrayItem) {
console.log(arrayItem.name);
console.log(arrayItem.url);
document.getElementById('buttonDiv').innerHTML += `<button onClick='openGame("${arrayItem.url}")'>${arrayItem.name}</button>`;
},);注意:
立即学习“Java免费学习笔记(深入)”;
为了按类别组织按钮,首先需要为每个类别创建一个 HTML 容器。例如:
<h1>Category 1</h1> <h1>Category 2</h1> <div id="buttonDiv"></div>
如果需要更灵活的类别管理,可以动态创建类别容器。首先,获取所有不同的类别:
const categories = [...new Set(buttonArr.map(item => item.category))];
然后,动态创建类别标题和容器:
const container = document.getElementById('container'); //假设有一个id为container的元素
categories.forEach(category => {
const h1 = document.createElement('h1');
h1.textContent = category;
container.appendChild(h1);
const div = document.createElement('div');
div.id = `buttonDiv-${category}`;
container.appendChild(div);
});接下来,修改 forEach 循环,将按钮添加到对应的类别容器中:
buttonArr.forEach(function (arrayItem) {
const categoryDivId = `buttonDiv-${arrayItem.category}`;
document.getElementById(categoryDivId).innerHTML += `<button onClick='openGame("${arrayItem.url}")'>${arrayItem.name}</button>`;
},);关键点:
openGame 函数用于在新窗口中打开游戏链接。
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 按钮并按类别组织它们:
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Buttons</title>
</head>
<body>
<div id="container"></div>
<script type="text/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://play2048.co/"}
];
const categories = [...new Set(buttonArr.map(item => item.category))];
const container = document.getElementById('container');
categories.forEach(category => {
const h1 = document.createElement('h1');
h1.textContent = category;
container.appendChild(h1);
const div = document.createElement('div');
div.id = `buttonDiv-${category}`;
container.appendChild(div);
});
buttonArr.forEach(function (arrayItem) {
const categoryDivId = `buttonDiv-${arrayItem.category}`;
document.getElementById(categoryDivId).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);
}
</script>
</body>
</html>通过以上步骤,可以有效地使用 JavaScript 动态生成 HTML 按钮,并根据其类别进行组织。 这种方法可以用于构建动态的游戏列表、应用商店或其他需要根据数据生成 UI 元素的场景。 记住要正确使用模板字符串、确保 HTML 元素存在,并合理地组织代码结构,以提高代码的可读性和可维护性。
以上就是使用 JavaScript 动态生成 HTML 元素并按类别组织的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号