
本文档旨在指导开发者如何使用 JavaScript 动态地根据数据生成 HTML 按钮,并按照预定义的类别对这些按钮进行组织。我们将通过一个实际案例,演示如何利用数组数据,结合 DOM 操作,高效地创建按钮元素,并将其放置到对应的类别容器中,最终实现一个可交互的游戏列表页面。
首先,我们需要一个包含按钮数据的数组。 数组中的每个元素都应包含按钮的类别、名称和 URL。 例如:
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://example.com/2048" }
];接下来,使用 forEach 循环遍历数组,并为每个元素创建一个按钮。使用模板字符串可以方便地将变量插入到 HTML 字符串中。
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 中预先定义这些容器,或者使用 JavaScript 动态创建。 这里展示预先定义的方式:
<h1>Category 1</h1> <div id="category1"></div> <h1>Category 2</h1> <div id="category2"></div>
然后,在循环遍历数组时,根据按钮的类别,将按钮添加到相应的容器中。
buttonArr.forEach(function (arrayItem) {
const categoryId = arrayItem.category.toLowerCase().replace(/\s+/g, ''); // 将类别转换为 ID 格式
const categoryDivId = `category${categoryId}`;
let categoryDiv = document.getElementById(categoryDivId);
// 如果类别容器不存在,则创建它
if (!categoryDiv) {
categoryDiv = document.createElement('div');
categoryDiv.id = categoryDivId;
document.body.appendChild(categoryDiv); // 或者添加到其他合适的父元素
const categoryHeader = document.createElement('h1');
categoryHeader.textContent = arrayItem.category;
document.body.insertBefore(categoryHeader, categoryDiv); // 在div之前插入标题
}
categoryDiv.innerHTML += `<button onClick='openGame("${arrayItem.url}")'>${arrayItem.name}</button>`;
});代码解释:
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Buttons</title>
</head>
<body>
<div id="buttonDiv"></div>
<script type="text/javascript">
//start game code
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://example.com/2048" }
];
buttonArr.forEach(function (arrayItem) {
const categoryId = arrayItem.category.toLowerCase().replace(/\s+/g, ''); // 将类别转换为 ID 格式
const categoryDivId = `category${categoryId}`;
let categoryDiv = document.getElementById(categoryDivId);
// 如果类别容器不存在,则创建它
if (!categoryDiv) {
categoryDiv = document.createElement('div');
categoryDiv.id = categoryDivId;
document.body.appendChild(categoryDiv); // 或者添加到其他合适的父元素
const categoryHeader = document.createElement('h1');
categoryHeader.textContent = arrayItem.category;
document.body.insertBefore(categoryHeader, categoryDiv); // 在div之前插入标题
}
categoryDiv.innerHTML += `<button onClick='openGame("${arrayItem.url}")'>${arrayItem.name}</button>`;
});
//end game code
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 元素的场景。 记住,清晰的代码结构、适当的错误处理和良好的性能优化是构建高质量 Web 应用的关键。
以上就是使用 JavaScript 动态生成按钮并按类别组织的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号