Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
html中的Javascript div输出不工作_Javascript_Html_Output_Innerhtml - Fatal编程技术网

html中的Javascript div输出不工作

html中的Javascript div输出不工作,javascript,html,output,innerhtml,Javascript,Html,Output,Innerhtml,我使用这个innerhtml属性将javascript输出打印到html中的div中。我已经检查了其他有同样问题的主题,但似乎没有任何效果 下面的代码都在我的html中的“body”标记之间 代码: <script> function firstGame(); </script> <div id="outputgame"> </div> <button onclick="functionGame()"> Starten </bu

我使用这个innerhtml属性将javascript输出打印到html中的div中。我已经检查了其他有同样问题的主题,但似乎没有任何效果

下面的代码都在我的html中的“body”标记之间

代码:

<script> function firstGame(); </script>

<div id="outputgame"> </div>
<button onclick="functionGame()"> Starten </button>

<script type="text/javascript">
    function firstGame()
    {
        var output = document.getElementById('outputgame');
        output.innerHTML('<p>First test</p>');

        // Check if the user is ready to play!
        confirm("Are you ready to play this epic game?")

        var age = prompt("What is your age?");
        if (age < 13)
        {
            document.getElementById('outputgame').innerHTML= "You are to young to play this  game. You can proceed, however i do not take any responsibility";

        }
        else
        {
            document.getElementById('outputgame').innerHTML= "Sweet, Let's go!";
        }
     }
 </script>
函数firstGame();
斯塔滕
函数firstGame()
{
var output=document.getElementById('outputgame');
output.innerHTML(“第一次测试”

”); //检查用户是否已准备好播放! 确认(“你准备好玩这个史诗游戏了吗?”) var age=prompt(“您的年龄是多少?”); if(年龄<13岁) { document.getElementById('outputgame').innerHTML=“你玩这个游戏太年轻了。你可以继续,但我不承担任何责任”; } 其他的 { document.getElementById('outputgame').innerHTML=“甜甜的,我们走吧!”; } }

斯塔滕
函数firstGame()
{
var output=document.getElementById('outputgame');
output.innerHTML='第一次测试

'; //检查用户是否已准备好播放! 确认(“你准备好玩这个史诗游戏了吗?”) var age=prompt(“您的年龄是多少?”); if(年龄<13岁) { document.getElementById('outputgame').innerHTML=“你玩这个游戏太年轻了。你可以继续,但我不承担任何责任”; } 其他的 { document.getElementById('outputgame').innerHTML=“甜甜的,我们走吧!”; } }
两个问题:您将函数命名为错误的名称?,并且您将调用
innerHTML
作为函数,而不是为其赋值

另外,在哪里定义了
functionGame()
?不能像在C中那样使用转发声明

函数firstGame(){
var output=document.getElementById('outputgame');
var text='';
output.innerHTML='第一次测试

'; //检查用户是否已准备好播放! 确认(“你准备好玩这个史诗游戏了吗?”) var age=parseInt(提示(“您的年龄是多少?”),10); if(年龄<13岁){ text=“你很年轻,可以玩这个游戏。你可以继续,但我不承担任何责任。”; }否则{ text=“亲爱的,我们走吧!”; } document.getElementById('outputgame')。innerHTML=text; }

Starten
HTML

<div id="outputgame"> </div>
<button onclick="firstGame()"> Starten </button>

斯塔滕
javascript

<script type="text/JavaScript">
function firstGame()
{
var output = document.getElementById('outputgame');
output.innerHTML='<p>First test</p>';

// Check if the user is ready to play!
confirm("Are you ready to play this epic game?")

var age = prompt("What is your age?");
if (age < 13)
{
document.getElementById('outputgame').innerHTML= "You are to young to play this  game.You can proceed, however i do not take any responsibility";

}
else
{
    document.getElementById('outputgame').innerHTML= "Sweet, Let's go!";
}
}

</script>

函数firstGame()
{
var output=document.getElementById('outputgame');
output.innerHTML='第一次测试

'; //检查用户是否已准备好播放! 确认(“你准备好玩这个史诗游戏了吗?”) var age=prompt(“您的年龄是多少?”); if(年龄<13岁) { document.getElementById('outputgame').innerHTML=“你玩这个游戏太年轻了。你可以继续,但我不承担任何责任”; } 其他的 { document.getElementById('outputgame').innerHTML=“甜甜的,我们走吧!”; } }
让我们谈谈初始化时的页面呈现和
全局范围。范围是值和表达式“可见”或可以引用的上下文<代码>全局范围
是整个
文档
的上下文。如果在这里声明了一个值或函数,那么它在每个函数或表达式中都是可见的

页面中的JavaScript一次解析一个脚本块。当脚本块在没有错误的情况下被解析时,脚本块被转移到全局范围。所有公共值和表达式都可用的线程(存储在窗口对象中)

首先解析函数,然后解析块的其余部分。但是,我在前面编写的脚本块一次解析一个。因此,如果调用一个尚未在全局范围内的函数,因为它位于另一个尚未解析的脚本块中,那么代码将失败并抛出未定义的错误

考虑这一点:

 <script>
     function test()
     {
         alert(1);
     }
     test();
 </script>
这也行得通。由于先解析函数,因此
test()
仍然有效并发出警报
1

 <script>
     test();
 </script>
 <script>
     function test()
     {
         alert(1);
     }

 </script>
这确实有效。首先声明函数并将其添加到全局范围。在第二个脚本块中,
test()
global scope
中查找函数
test
,找到它并执行它

以上解释适用于您的代码:

<div id="outputgame"> </div>
<button onclick="functionGame()"> Starten </button>

<script type="text/javascript">
    function firstGame()
    {
        var output = document.getElementById('outputgame');
        output.innerHTML = '<p>First test</p>';

        // Check if the user is ready to play!
        // Do you really need this?
        //Confirm returns a Boolean (true or false), you can do something with the result. If not just show an alert or nothing. Latter is preferred. 
        confirm("Are you ready to play this epic game?")

        var age = prompt("What is your age?");
        //You should make this more robust. What if someone enters "asaaslk". Use a select box with birth years.
        if (age < 13)
        {
            document.getElementById('outputgame').innerHTML = "You are to young to play this  game. You can proceed, however i do not take any responsibility";

        }
        else
        {
            document.getElementById('outputgame').innerHTML = "Sweet, Let's go!";
        }
     }
 </script>
 <script> function firstGame(); </script>

斯塔滕
函数firstGame()
{
var output=document.getElementById('outputgame');
output.innerHTML='第一次测试

'; //检查用户是否已准备好播放! //你真的需要这个吗? //Confirm返回一个布尔值(true或false),您可以对结果进行处理。如果不只是显示警报或什么都不显示,则最好使用后者。 确认(“你准备好玩这个史诗游戏了吗?”) var age=prompt(“您的年龄是多少?”); //你应该让它更强大。如果有人输入“asalk”该怎么办。使用带有出生年份的选择框。 if(年龄<13岁) { document.getElementById('outputgame').innerHTML=“你玩这个游戏太年轻了。你可以继续,但我不承担任何责任”; } 其他的 { document.getElementById('outputgame').innerHTML=“甜甜的,我们走吧!”; } } 函数firstGame();
头部是否有对jQuery的引用?对
firstGame()
的调用发生在不同的脚本块中,然后函数被调用。因此,当调用
firstGame
时,会返回一个脚本错误,因为没有定义
firstGame
。将调用移动到第二个脚本块。您不需要jQuery…请删除确认,因为它显然不是必需的,而且非常烦人。将您的提示包装在parseInt(提示(“您的年龄是多少?”)中;代码–只有答案不受欢迎。你应该解释为什么OP有这个问题,为什么你的a
 <script>
     function test()
     {
         alert(1);
     }

 </script>
 <script>
     test();
 </script>
<div id="outputgame"> </div>
<button onclick="functionGame()"> Starten </button>

<script type="text/javascript">
    function firstGame()
    {
        var output = document.getElementById('outputgame');
        output.innerHTML = '<p>First test</p>';

        // Check if the user is ready to play!
        // Do you really need this?
        //Confirm returns a Boolean (true or false), you can do something with the result. If not just show an alert or nothing. Latter is preferred. 
        confirm("Are you ready to play this epic game?")

        var age = prompt("What is your age?");
        //You should make this more robust. What if someone enters "asaaslk". Use a select box with birth years.
        if (age < 13)
        {
            document.getElementById('outputgame').innerHTML = "You are to young to play this  game. You can proceed, however i do not take any responsibility";

        }
        else
        {
            document.getElementById('outputgame').innerHTML = "Sweet, Let's go!";
        }
     }
 </script>
 <script> function firstGame(); </script>