gpt4 book ai didi

javascript - 输入 "no"后窗口不会关闭? JavaScript

转载 作者:行者123 更新时间:2023-11-28 04:14:17 25 4
gpt4 key购买 nike

有人可以帮我找出代码中的错误吗?完成游戏后输入“否”时,窗口不会关闭,我似乎找不到错误。对于上下文:我试图制作一款战舰游戏并使其与 javascript 交互。当船被击中时,用户应该可以选择再次玩(窗口重新加载就好),如果用户决定不再玩,他只需输入“否”,窗口就会关闭,但是由于某种原因这不起作用。无论我做什么,我似乎都找不到错误。有经验丰富的人有想法吗?

var table = document.getElementById("buttons");

var len_board = document.getElementsByClassName("tr").length;

var button = document.getElementsByClassName("location")

//Places ships on the board randomly
function placeShips(len_board) {
window.random_row = Math.floor(Math.random() * (len_board - 1)) + 1;
window.random_column = Math.floor(Math.random() * (len_board - 1)) + 1;
};


//Save users choice and determine if ship is hit
function getId(element) {
var chosen_row = element.parentNode.parentNode.rowIndex
var chosen_column = element.parentNode.cellIndex

if ((random_row == chosen_row) && (random_column == chosen_column)) {
element.style.backgroundColor = 'red';
element.style.borderColor = 'red';
element.textContent = "X";
alert("you hit my ship");
var answer = prompt("Would you like to play again? Enter 'Yes' for
yes and 'No' for no:");
var answer = answer.toLowerCase();
if (answer == "yes") {
location = location;
}

else if (answer == "no") {
window.close();
}

else {
var reply = prompt("I'm sorry, " + answer + " is not a valid
input. Please try again: ");
var reply = reply.toLowerCase();
if (reply == "no") {
window.close();
}
else if (answer == "yes") {
location = location;
}
else {
window.close();
};
};
}

else {
element.style.backgroundColor = "#a9c8f9";
element.style.borderColor = "#a9c8f9";
};
};

最佳答案

脚本只能关闭该脚本打开的窗口。您无法关闭加载脚本的主窗口。

关于javascript - 输入 "no"后窗口不会关闭? JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46008570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com