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

时间:2016-05-28 15:24:15

标签: javascript dom textbox alert

我有一个使用窗口警报的作业,因此用户知道游戏中发生了什么。我现在必须为不同的任务更新该游戏,但这次窗口警报必须进入文本框。文本框中有“单击您的选择”作为基本文本。如何使窗口警报覆盖此文本?

以下代码是我对窗口警报的代码:

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();
        }
    }
}

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

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

有人可以告诉我如何做到这一点吗?

1 个答案:

答案 0 :(得分:1)

一个简单的方法是你可以在你的javascript中直接覆盖窗口的警报功能,并且在覆盖的函数内部获取该文本框的id并在那里添加警报值...... 例如

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

这也是您尝试的fiddle