1

这里的目标是,当我在文本框中输入一个值时,它会根据你之间的年份为你分配一个标题。但是,它不起作用,JSFiddle 给了我以下错误:

 "<a class='gotoLine' href='#48:26'>48:26</a> Uncaught SyntaxError:
 Unexpected token '&lt;='"

据我所知,这甚至不应该是一个错误。小于或等于的符号是<=所以我不确定问题是什么。

function ShowGeneration() {
  var yearBorn;
  var generation;
  var Boomer
  var X
  var Millenial
  var Z
  yearBorn = parseFloat(document.getElementById('yearBox').value);

  // insert if statements here to map yearBorn onto generation
  if (yearBorn >= 1944 && yearBorn <= 1964)
    generation = Boomer;
  else {
    if (yearBorn >= 1965 && yearBorn <= 1979)
      generation = X;
    else {
      if (yearBorn >= 1980 && yearBorn <= 1994)
        generation = Millenial;
      else {
        if (yearBorn >= 1995 && yearBorn <= 2015)
          generation = Z;
      }
    }
  }



  document.getElementById('outputDiv').innerHTML = "You belong to the " + generation + " generation.";
}
<p>
  What year were you born? <input type="text" id="yearBox" size="6">
</p>
<input type="button" value="Click for Generation" onclick="ShowGeneration();">

<div id="outputDiv"></div>

4

2 回答 2

1

修正了一些语法和格式,见下文。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Generations</title>
        <script type="text/javascript">

            function showGeneration() {

                var generation;

                var yearBorn = parseFloat(document.getElementById('yearBox').value);

                // insert if statements here to map yearBorn onto generation
                if (yearBorn >= 1944 && yearBorn <= 1964) {
                  generation = "Boomer";
                }
                else if (yearBorn >= 1965 && yearBorn <= 1979) {
                  generation = "X";
                }
                else if (yearBorn >= 1980 && yearBorn <= 1994) {
                  generation = "Millenial";
                }
                else if (yearBorn >= 1995 && yearBorn <= 2015) {
                  generation = "Z";
                }  

                document.getElementById('outputDiv').innerHTML = "You belong to the " + generation + " generation.";
            }
        </script>
    </head>

    <body>
      <p>
        What year were you born? <input type="text" id="yearBox" size="6">
      </p>
      <input type="button" value="Click for Generation"  onclick="showGeneration();">
      <div id="outputDiv"></div>
    </body>
</html>

JSFiddle

请注意,任何年龄小于1944或大于2015的人仍然属于这undefined一代;if你应该在你的陈述中介绍这两种情况。

于 2021-04-23T19:37:27.887 回答
0

我发现错误:

  1. generation 的值必须在引号之间,因为它们是字符串

  2. 在比较中缺少年份出生

  3. 缺少花括号来关闭 if 和函数

尝试这个

      <!DOCTYPE html>
<html>

  <head>
    <meta charset="ISO-8859-1">
    <title>Generations</title>
    <script type="text/javascript">
      function ShowGeneration() {
        var yearBorn;
        var generation;
        yearBorn = parseFloat(document.getElementById('yearBox').value);

        // insert if statements here to map yearBorn onto generation
        if (yearBorn >= 1944 && yearBorn <= 1964)
          generation = "Boomer";
        else {
          if (yearBorn >= 1995 && yearBorn <= 1979)
            generation = "X";
          else {
            if (yearBorn >= 1980 && yearBorn <= 1994)
              generation = "Millenial";
            else {
              if (yearBorn >= 1995 && yearBorn <= 2015)
                generation = "Z";

              document.getElementById('outputDiv').innerHTML = "You belong to the " + generation + " generation.";
            }
          }

        }
      }

    </script>
  </head>

  <body>
    <p>
      What year were you born? <input type="text" id="yearBox" size="6">
    </p>
    <input type="button" value="Click for Generation" onclick="ShowGeneration()">

    <div id="outputDiv"></div>
  </body>

</html>
于 2021-04-23T19:29:29.323 回答