【问题标题】:whats going wrong with my code? [closed]我的代码出了什么问题? [关闭]
【发布时间】:2016-02-27 02:50:54
【问题描述】:

我在这里有一些代码,我正在努力找出它出了什么问题,我知道一些 javascript 是错误的,但是我将如何纠正它,非常感谢

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Numbers Games</title>
<link rel="stylesheet" type="text/css" href="style-home.css">
</head>

<body>
<div id="header"
<h1>Numbers Game</h1>
</div>
<p id="ParQuestion">What is five thousand and thirty three in numbers.</p>
<input id="txt_Ans" type="text" />
<input id="btn_Start" type="button" value="Start"                   

onclick="btn_Start_OnClick()" /><br />  
<input id="btn_Check" type="button" value="Check"      onclick="btn_Check_OnlClick()" />


<div id="Scoreboard">
<img id="scoreOn1" src="Pictures/Scorefaceoff1.gif" height="50px"        width="50px" /> 
<img id="scoreOn2" src="Pictures/Scorefaceoff2.gif" height="50px" width="50px" /> 
<img id="scoreOn3" src="Pictures/Scorefaceoff3.gif" height="50px" width="50px" /> 
<img id="scoreOn4" src="Pictures/Scorefaceoff4.gif" height="50px" width="50px" /> 
<img id="scoreOn5" src="Pictures/Scorefaceoff5.gif" height="50px" width="50px" />
</div>

<p id="ParComment"></p>

</body>


</html>
<script language="javascript">

    function btn_Check_OnClick(){
       if txt_Ans.value = "5033"
        parRes.InnerText=ParComment.value;
    }else{
        ParComment.InnerText="Wrong, Try Again";
    }
    }
</sript>

【问题讨论】:

  • 欢迎来到 Stackoverflow。你认为那是什么 ?到目前为止你做了什么 ?为了让我们帮助你证明你做了实际的研究。 :)
  • 话虽如此,btn_Check_onlClick()?该特定功能在哪里定义?你的脚本块中有一个额外的}

标签: javascript arrays function loops var


【解决方案1】:

首先你的“&lt;/sript&gt;”标签->“&lt;/script&gt;

if txt_Ans.value = "5033"”->“if (txt_Ans.value == "5033"){ parRes.InnerText=ParComment.value; }

关闭第 10 行的“div-tag”。

编辑:首先尝试使用“http://jshint.com/”之类的代码检查器来检查语法错误

【讨论】:

    【解决方案2】:

    只是评论脚本块:

    • 你的结束标签是“sript”,而不是“script”
    • 您的 if 语句有语法错误,缺少 (, ) 和 {
    • 您的 if 语句正在指定应该比较的时间;在条件中使用 == over =
    • 该属性称为innerText,而不是InnerText
    • ParComment 没有 value 属性

    【讨论】:

      【解决方案3】:

      我认为您没有添加开始脚本标记。此外,您应该在 if 语句中添加括号。而不是使用= 来比较使用==。另外,使用document.getElementById获取元素

      试试这个:

      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8" />
      <title>Numbers Games</title>
      <link rel="stylesheet" type="text/css" href="style-home.css">
      </head>
      
      <body>
      <div id="header"
      <h1>Numbers Game</h1>
      </div>
      <p id="ParQuestion">What is five thousand and thirty three in numbers.</p>
      <input id="txt_Ans" type="text" />
      <input id="btn_Start" type="button" value="Start"                   
      
      onclick="btn_Start_OnClick()" /><br />  
      <input id="btn_Check" type="button" value="Check"      onclick="btn_Check_OnlClick()" />
      
      
      <div id="Scoreboard">
      <img id="scoreOn1" src="Pictures/Scorefaceoff1.gif" height="50px"        width="50px" /> 
      <img id="scoreOn2" src="Pictures/Scorefaceoff2.gif" height="50px" width="50px" /> 
      <img id="scoreOn3" src="Pictures/Scorefaceoff3.gif" height="50px" width="50px" /> 
      <img id="scoreOn4" src="Pictures/Scorefaceoff4.gif" height="50px" width="50px" /> 
      <img id="scoreOn5" src="Pictures/Scorefaceoff5.gif" height="50px" width="50px" />
      </div>
      
      <p id="ParComment"></p>
      
      <script>
          function btn_Check_OnClick(){
             if(document.getElementById('txt_Ans').value == "5033"){
                 parRes.InnerText=ParComment.value;
             }else{
                 document.getElementById('ParComment').innerHTML = "Wrong, Try Again";
             }
          }
      </sript>
      </html>
      

      最好将脚本放在 html 标签中

      【讨论】:

        【解决方案4】:

        其他人没有提到的一件事是&lt;div id="header" 缺少结束&gt;。不过,您有很多其他人确实提到过的语法错误。

        在您的浏览器开发工具中查看这一点会让您受益匪浅。对于大多数现代浏览器,只需按 F12 即可打开它们。我相信您可能会在控制台中看到一些 JavaScript 错误。这是一个很好的起点。

        对于您的其他语法错误,这里有几个地方可以查看:

        【讨论】:

          猜你喜欢
          • 2013-03-06
          • 2013-05-12
          • 2015-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-11
          • 1970-01-01
          • 2023-04-01
          相关资源
          最近更新 更多