【问题标题】:Why might my javascript not be working?为什么我的 javascript 无法正常工作?
【发布时间】:2016-11-15 23:41:47
【问题描述】:

只显示脚本上方的 html 文本;提示永远不会出现。我无法弄清楚缺少/错误的内容。也是家庭作业,我没编主题/内容哈哈。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Part 1 Problem 6</title>
    </head>
    <body>
        <h1>JS Lab 1 Question Answers</h1>
        <ol>
            <li>document.write is the script that tells the browser to write certain text. document.getElementById allows you to change certain text that is already written by calling it by its id. Using document.getElementById, you can modify a large group of your code at the same time.</li>
            <li>A variable holds the place of a certain value in your code, like a number or a name. An array holds multiple values</li>
            <li>If there was no variable with the prompt, you would still get the prompt but nothing would be done about it. What you enter into the prompt becomes the value of the variable.</li>
            <!--<script>document.write("<li>To generate a random number between 50 and 350 (not including 350) you would write var randnum (or whatever you name your variable) = (Math.floor(Math.random() * 300) + 50)</li>")</script>-->
            <li>If you generate 4 different random numbers, you should have at least 4 variables.</li>
        </ol>
        <script>
            var insultarray = new Array();
            insultarray[0] = "You smell bad";
            insultarray[1] = "You are ugly";
            insultarray[2] = "I just really don't like you";
            insultarray[3] = "You are annoying";
            insultarray[4] = "Your hair looks bad";
            var randnum = (Math.floor(Math.random()*5);
            var x = prompt("Want to know what I think?","yes");
            if (x == "yes")
            {
                document.write("<p>" + insultarray[randnum] + "</p>");
            }
            else
            {
                document.write("<p>Honestly, I like you.</p>");
            }
        </script>
    </body>
</html>

【问题讨论】:

  • 浏览器开发者工具控制台中是否出现任何错误?
  • var randnum = (Math.floor(Math.random()*5);
  • var randnum = (Math.floor(Math.random()*5); 你错过了另一个) 检查你的浏览器控制台是否有错误。而且 document.write 并不是真正弄乱 DOM 的理想方法。
  • 如果您还不熟悉浏览器的控制台/检查器,请熟悉它们!您将很容易找到像这样的错误。

标签: javascript arrays prompt


【解决方案1】:

Math.floor(Math.random()*5) 中有一个前括号,我在下面删除了它,现在可以使用了。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Part 1 Problem 6</title>
    </head>
    <body>
        <h1>JS Lab 1 Question Answers</h1>
        <ol>
            <li>document.write is the script that tells the browser to write certain text. document.getElementById allows you to change certain text that is already written by calling it by its id. Using document.getElementById, you can modify a large group of your code at the same time.</li>
            <li>A variable holds the place of a certain value in your code, like a number or a name. An array holds multiple values</li>
            <li>If there was no variable with the prompt, you would still get the prompt but nothing would be done about it. What you enter into the prompt becomes the value of the variable.</li>
            <!--<script>document.write("<li>To generate a random number between 50 and 350 (not including 350) you would write var randnum (or whatever you name your variable) = (Math.floor(Math.random() * 300) + 50)</li>")</script>-->
            <li>If you generate 4 different random numbers, you should have at least 4 variables.</li>
        </ol>
        <script>
            var insultarray = new Array();
            insultarray[0] = "You smell bad";
            insultarray[1] = "You are ugly";
            insultarray[2] = "I just really don't like you";
            insultarray[3] = "You are annoying";
            insultarray[4] = "Your hair looks bad";
            var randnum = Math.floor(Math.random()*5);
            var x = prompt("Want to know what I think?","yes");
            if (x == "yes")
            {
                document.write("<p>" + insultarray[randnum] + "</p>");
            }
            else
            {
                document.write("<p>Honestly, I like you.</p>");
            }
        </script>
    </body>
</html>

【讨论】:

    【解决方案2】:
    var randnum = (Math.floor(Math.random()*5); 
    

    Math.floor 前面有个小括号,去掉。

    【讨论】:

      【解决方案3】:

      大声笑,你的剧本有人类的解释,更像是一个伤害问题 顺便说一句,你只是漏掉了一个括号

      var insultarray = new Array();
      insultarray[0] = "You smell bad";
      insultarray[1] = "You are ugly";
      insultarray[2] = "I just really don't like you";
      insultarray[3] = "You are annoying";
      insultarray[4] = "Your hair looks bad";
      var randnum = (Math.floor(Math.random()*5));
      var x = prompt("Want to know what I think?","yes");
      if (x == "yes")
      {
        document.write("<p>" + insultarray[randnum] + "</p>");
      }
      else
      {
        document.write("<p>Honestly, I like you.</p>");
      }
      <h1>JS Lab 1 Question Answers</h1>
      <ol>
        <li>document.write is the script that tells the browser to write certain text. document.getElementById allows you to change certain text that is already written by calling it by its id. Using document.getElementById, you can modify a large group of your code at the same time.</li>
        
        <li>A variable holds the place of a certain value in your code, like a number or a name. An array holds multiple values</li>
      
        <li>If there was no variable with the prompt, you would still get the prompt but nothing would be done about it. What you enter into the prompt becomes the value of the variable.</li>
      
        <li>If you generate 4 different random numbers, you should have at least 4 variables.</li>
      </ol>

      【讨论】:

        【解决方案4】:

        您忘记在第 23 行添加右括号,应该是:

        var randnum = Math.floor(Math.random()*5);

        这些错误可以通过查看控制台发现。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-08-26
          • 2016-03-30
          • 1970-01-01
          • 1970-01-01
          • 2012-12-28
          • 2021-12-30
          • 2020-06-27
          相关资源
          最近更新 更多