![](/img/trans.png)
[英]How can I get the positions of a for ( … in … ) loop in javascript?
[英]How can I make a button change Javascript array positions?
我需要制作一個能夠替換數組位置(文本)的按鈕。 例如,數組位置3需要變成數組位置1。它只需要工作一次,盡管總是歡迎學習如何使其多次執行。 它必須通過for循環來完成。 如果取模是一種解決方案,請問給我解釋一下,因為我不太確定它是如何工作的。 這是我的代碼:
<body>
<button type='button' onclick="Husselaar()">Husselen!</button>
<br>
<script>
var games = ["Minecraft", "Assassin's Creed", "Rise Of The Tomb Raider", "Far Cry", "Tom Clancy's Rainbow Six Siege", "Call of Duty", "Grand Theft Auto V", "Hotline Miami", "American Truck Simulator", "Life is Strange"];
var arrayLength = games.length;
for (var i = 0; i < arrayLength; i++) {
document.write(games[i] + "<br>");
}
function Husselaar(){
var husselaar = document.getElementById("");
Math.floor((Math.random(husselaar) * 9)+ 1);
}
</script>
</body>
提前致謝! :)
您可以使用Array.splice(startposition,amount)提取數組的一部分。
使用Array.concat將舊數組與結果數組合並。
var games = ["Minecraft", "Assassin's Creed", "Rise Of The Tomb Raider", "Far Cry", "Tom Clancy's Rainbow Six Siege", "Call of Duty", "Grand Theft Auto V", "Hotline Miami", "American Truck Simulator", "Life is Strange"]; function toStart() { var promptStr = "Choose the game to send to start:\\n"; for (var i = 0; i < games.length; i++) { promptStr += i + ": " + games[i] + "\\n" } var index = parseInt(prompt(promptStr)); games = games.splice(index, 1).concat(games); console.log(games); }
<button type='button' onclick="toStart()">Husselen!</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.