如何将所有 window 警报发送到一个文本框

How do I send all window alerts to one text box

我的作业使用了 window 警报,因此用户知道游戏中发生了什么。我现在必须为不同的任务更新该游戏,但这次 window 警报必须进入文本框。文本框以 "Click on your selection" 作为基础文本。如何让 window 警报覆盖此文本?

下面的代码是 window 警报的代码:

function check(guess) {
    if (diamond == guess) {
        window.alert("Congratulations! You have found the diamond.")
        again = window.prompt("Would you like to play another game? Enter Y or N.", "y");
        if (again == "N" || again == "n") {
            window.alert("Thanks for playing.Goodbye.");
            window.close();
        } else {
            window.alert("The diamond has been hidden. You can now try again.");
            window.location.reload();
        }
    } else {
        number_of_guesses = number_of_guesses + 1;
        if (diamond < guess) {
            result = "lower"
        } else {
            result = "higher"
        }
        window.alert("Guess number " + number_of_guesses + " is incorrect. Diamond is " + result + ".");
    }
    if (number_of_guesses >= 3) {
        window.alert("Sorry, you have run out of guesses! The diamond was in box " + diamond);
        again = window.prompt("Would you like to play again? Enter Y or N. ", "y");
        if (again == "N" || again == "n") {
            window.alert("Thanks for playing. Goodbye.");
            window.close();
        } else {
            window.alert("The diamond has been hidden. You can now try again.");
            window.location.reload();
        }
    }
}

我现在要将所有 window 警报放入一个文本框中:

<INPUT TYPE="text" id="windowalerts" NAME="windowalerts" VALUE= "Click on a Selection " SIZE=30> 

谁能告诉我该怎么做?

一种简单的方法是您可以直接在 javascript 中覆盖 window 的警报功能,并在该覆盖的功能中获取该文本框的 ID 并在那里添加警报值... 例如

<input type="text" id="mytextbox"/>
window.alert = function(message) {
    document.getElementById('mytextbox').value = message;
}

这里还有一个fiddle供你尝试