用于显示函数JavaScript的文本框

用于显示函数JavaScript的文本框,javascript,Javascript,我对这学期的第一个项目有意见。我应该使用JavaScript创建一个神奇的8球模拟器。模拟器需要一个写着“摇晃它”的按钮,当点击这个按钮时,它会在一个只读文本框中显示来自我的GetSaying()函数的随机语句。这就是我迷路的地方。如何指定要在其中显示的文本框 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Magic 8 Ball!<

我对这学期的第一个项目有意见。我应该使用JavaScript创建一个神奇的8球模拟器。模拟器需要一个写着“摇晃它”的按钮,当点击这个按钮时,它会在一个只读文本框中显示来自我的GetSaying()函数的随机语句。这就是我迷路的地方。如何指定要在其中显示的文本框

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Magic 8 Ball!</title>
<style type="text/css">
/*Styles will go here*/


</style>
</head>
<body>

<button type="button" onclick="Show()">Shake it!</button>


<script type="text/javascript">

        function Show() {
            document.write(GetSaying());
        }

        function GetSaying() {

            var pool = ["It is certain", "It is decidedly so", "Without a doubt", 
                        "Yes definitely", "You may rely on it", "As I see it, yes",
                        "Most likely", "Outlook good", "Yes", "Signs point to yes", 
                        "Reply hazy try again", "Ask again later", "Better not tell you now", 
                        "Cannot predict now", "Concentrate and ask again", "Don't count on it", 
                        "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"];

            return (pool[Math.floor(Math.random()*pool.length)]);
        }

</script>


</body>
</html>

魔法8球!
/*风格会在这里流行*/
摇一摇!
函数Show(){
document.write(GetSaying());
}
函数GetSaying(){
var pool=[“这是肯定的”、“这是肯定的”、“毫无疑问的”,
“是的,当然”,“你可以信赖它”,“在我看来,是的”,
“最有可能”、“前景良好”、“是”、“迹象表明是”,
“回复模糊不清,再试一次”,“稍后再问”,“最好现在不要告诉你”,
“现在无法预测”,“集中精力再次提问”,“不要指望它”,
“我的回答是否定的”、“我的消息来源说否定的”、“前景不太好”、“非常可疑”];
返回(pool[Math.floor(Math.random()*pool.length)];
}

您必须使用
document.getElementById()
获取
输入,然后设置其值

工作示例

摇一摇! 函数Show(){ document.getElementById(“saying”).value=getsayid(); } 函数GetSaying(){ var pool=[“这是肯定的”、“这是肯定的”、“毫无疑问的”, “是的,当然”,“你可以信赖它”,“在我看来,是的”, “最有可能”、“前景良好”、“是”、“迹象表明是”, “回复模糊不清,再试一次”,“稍后再问”,“最好现在不要告诉你”, “现在无法预测”,“集中精力再次提问”,“不要指望它”, “我的回答是否定的”、“我的消息来源说否定的”、“前景不太好”、“非常可疑”]; 返回(pool[Math.floor(Math.random()*pool.length)]; }
您可以使用Thanke@ChaoticNadirs定义一个文本框虽然我上面的代码没有反映这一点,但我的HTML中有一个文本框,我只是不知道如何从GetSaying()获取返回的值以显示在我的文本框中谢谢@Dave,我对此表示感谢。看到代码和它的实际工作方式帮了我很大的忙。我还有很多事情要做,但我已经把这些都记下来了。再次感谢。
<button type="button" onclick="Show()">Shake it!</button>
<input id="saying" />

<script type="text/javascript">

    function Show() {
        document.getElementById("saying").value = GetSaying();
    }

    function GetSaying() {

        var pool = ["It is certain", "It is decidedly so", "Without a doubt", 
                    "Yes definitely", "You may rely on it", "As I see it, yes",
                    "Most likely", "Outlook good", "Yes", "Signs point to yes", 
                    "Reply hazy try again", "Ask again later", "Better not tell you now", 
                    "Cannot predict now", "Concentrate and ask again", "Don't count on it", 
                    "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"];

        return (pool[Math.floor(Math.random()*pool.length)]);
    }

</script>