【问题标题】:Just starting java script, need help running both scripts, only one seems to run刚启动java脚本,需要帮助运行两个脚本,似乎只有一个运行
【发布时间】:2017-09-08 09:06:17
【问题描述】:

需要帮助运行这两个脚本,似乎只有一个在运行,我得到一个名称,但它只是停止运行。帮助将不胜感激。我一直在使用 java 和 C++,所以这自然让我感到困惑。谢谢

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script src="jstut.js"></script>

    <style type="text/css">
      body {font-size: 1.6em;}
      .hidden {display:none;}
      .show {display:inline !important;}
      button {
        border: 2px solid black; background: #E5E4E2;
        font-size: .5em; font-weight: bold; color: black;
        padding: .8em 2em;
        margin-top: .4em;
      }
    </style>
  </head>
  <body>
    <p id="sayHello"></p>

    <script> 
      var yourName = prompt("What is your name?");
      if(yourName!= null){
        document.getElementById("sayHello").innerHTML = "Hello " + yourName;
      }else{
        alert("Please enter your name correctly");
      }
    </script>

    <script> 
      var myAge = prompt("What is your age");
      if(myAge < 4){
        document.write ("You should be in preschool";
      }else if(my age > 4 && <18){
        document.write("You should be in public private school");
      }else if (my age >18 && <24){
        document.write("You should be in college");
      }
      else{ document.write(your in the work force now);}
    </script>
  </body>
</html>

【问题讨论】:

  • 在浏览器中按 F12 并查看 Console,您会看到一个错误,告诉您这里缺少 )("You should be in preschool";
  • 学习使用一些工具来验证你的 JS 语法:stackoverflow.com/questions/2120093/…
  • 另外,document.write(your in the work force now); 应该是 document.write("your in the work force now");
  • @Henry 从技术上讲,它应该是“you're 现在在工作中。”
  • @Albzi 很惊讶你没有指出技术上What is your age 缺少?

标签: javascript html


【解决方案1】:

您在此行错过了)document.write ("You should be in preschool";

@Albzi、@Alex K. 和 @Henry 在 cmets 中还指出了其他一些错误。这些人帮了很多忙。

代码更改:

  • 修复了此行中) 的问题:document.write ("You should be in preschool";
  • if(my age &gt; 4 &amp;&amp; &lt;18) 替换为if (myAge&gt;4 &amp;&amp; myAge&lt;18)。下面的行也是如此。
  • my age 替换为myAge
  • 更正拼写

body {
  font-size: 1.6em;
}

.hidden {
  display: none;
}

.show {
  display: inline!important;
}

button {
  border: 2px solid black;
  background: #E5E4E2;
  font-size: .5em;
  font-weight: bold;
  color: black;
  padding: .8em 2em;
  margin-top: .4em;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <script src="jstut.js"></script>
</head>

<body>
  <p id="sayHello"></p>

  <script>
    var yourName = prompt("What is your name?");
    if (yourName != null) {
      document.getElementById("sayHello").innerHTML = "Hello " + yourName;
    } else {
      alert("Please enter your name correctly");
    }
  </script>

  <script>
    var myAge = prompt("What is your age?");
    if (myAge < 4) {
      document.write("You should be in preschool");
    } else if (myAge > 4 && myAge < 18) {
      document.write("You should be in public private school");
    } else if (myAge > 18 && myAge < 24) {
      document.write("You should be in college");
    } else {
      document.write("You're in the work force now");
    }
  </script>
</body>

</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多