簡體   English   中英

如何為我的腳本創建一個具有 2 個不同標簽的數組,以便我可以識別游戲名稱和 url 以將其加載到同一頁面中?

[英]How do I make an Array for my script with 2 different labels so I can id the game name and url to load it in the same page?

 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 它將返回其值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM