function reply_click(clicked_id) {
console.log(clicked_id);
var urlObj = new window.URL(window.location.href);
var url = clicked_id;
if (url) {
var win;
document.querySelector('button').onclick = function() {
if (win) {
win.focus();
} else {
win = window.open();
win.document.body.style.margin = '0';
win.document.body.style.height = '100vh';
var 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);
}
document.querySelector('button').style.background = '#00000';
};
}
}
<div class="gameslist">
<h1>Action Games</h1>
<button id="https://theadvancedsociety-tam.tbt.mx/tam-run3/" onClick="reply_click(this.id)">Run 3</button><br>
<button id="https://bigfoot9999.github.io/Slope-Game/" onClick="reply_click(this.id)">Slope Game</button><br>
<h1>Puzzle & Strategy Games</h1>
<h1>Skill Games</h1>
<h1>Retro</h1>
<h1>Shooter Games</h1>
<h1>Other</h1>
</div>
我如何得到第二个按钮加载脚本,每次加载正确的URL,似乎按钮不工作的第一次用户按下它?而且,一定有更好的方法来处理数组,我从来没有学过数组,所以我需要帮助。
我在您的示例中没有找到任何数组,老实说,我不知道您想如何使用它。但只是在新窗口打开游戏-这里是一个固定的代码:
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);
}
<div className="gameslist">
<h1>Action Games</h1>
<button onClick="openGame('https://theadvancedsociety-tam.tbt.mx/tam-run3/')">Run 3</button>
<br>
<button onClick="openGame('https://bigfoot9999.github.io/Slope-Game/')">Slope Game</button>
<br>
<h1>Puzzle & Strategy Games</h1>
<h1>Skill Games</h1>
<h1>Retro</h1>
<h1>Shooter Games</h1>
<h1>Other</h1>
</div>
我不确定我是否理解对你的问题但如果你想有一个对象,你可以保存游戏名称和url你可以使用json而不是数组,例如:
const game = {
"name": "Slope Game",
"url": "https://bigfoot9999.github.io/Slope-Game/"
}
更容易访问信息,您只需要像这样调用它:game.name或game。Url,它将返回它的值。