简体   繁体   English

此 JavaScript 代码不起作用。 我的语法错误还是其他原因?

[英]This JavaScript code is not working. Have I got the syntax wrong or is it something else?

This code is not working as I would have liked it to, but I am not sure of the problem.这段代码没有像我希望的那样工作,但我不确定是什么问题。


Code:代码:

<body>
  var randNumForQuote = Math.floor((Math.random() * 11));

  if (randNumForQuote == 0) {
    document.getElementById("quoteDiv").innerHTML = "Hello";
  } else if (randNumForQuote == 1) {
    document.getElementById("quoteDiv").innerHTML = "Hello1";
  } else if (randNumForQuote == 2) {
    document.getElementById("quoteDiv").innerHTML = "Hello2";
  } else if (randNumForQuote == 3) {
    document.getElementById("quoteDiv").innerHTML = "Hello3";
  } else if (randNumForQuote == 4) {
    document.getElementById("quoteDiv").innerHTML = "Hello4";
  } else if (randNumForQuote == 5) {
    document.getElementById("quoteDiv").innerHTML = "Hello5";
  } else if (randNumForQuote == 6) {
    document.getElementById("quoteDiv").innerHTML = "Hello6";
  } else if (randNumForQuote == 7) {
    document.getElementById("quoteDiv").innerHTML = "Hello7";
  } else if (randNumForQuote == 8) {
    document.getElementById("quoteDiv").innerHTML = "Hello8";
  } else if (randNumForQuote == 9) {
    document.getElementById("quoteDiv").innerHTML = "Hello9";
  } else if (randNumForQuote == 10) {
    document.getElementById("quoteDiv").innerHTML = "Hello10";
  }

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


I have limited experience in JavaScript, so do not understand the problem too well.我对 JavaScript 的经验有限,所以不太了解这个问题。

I am expecting the div to say one of the outputs (eg. Hello, Hello1, Hello2, etc. )我期待 div 说出输出之一(例如, Hello, Hello1, Hello2, etc.

You need to put the JavaScript code inside script tag and run the code after page is loaded so put inside window.onload callback, which fires at the end of the document loading process.您需要将 JavaScript 代码放在script标签中,并在页面加载后运行代码,因此放在window.onload回调中,该回调在文档加载过程结束时触发。

 <body> <script> window.onload = function() { var randNumForQuote = Math.floor((Math.random() * 11)); if (randNumForQuote == 0) { document.getElementById("quoteDiv").innerHTML = "Hello"; } else if (randNumForQuote == 1) { document.getElementById("quoteDiv").innerHTML = "Hello1"; } else if (randNumForQuote == 2) { document.getElementById("quoteDiv").innerHTML = "Hello2"; } else if (randNumForQuote == 3) { document.getElementById("quoteDiv").innerHTML = "Hello3"; } else if (randNumForQuote == 4) { document.getElementById("quoteDiv").innerHTML = "Hello4"; } else if (randNumForQuote == 5) { document.getElementById("quoteDiv").innerHTML = "Hello5"; } else if (randNumForQuote == 6) { document.getElementById("quoteDiv").innerHTML = "Hello6"; } else if (randNumForQuote == 7) { document.getElementById("quoteDiv").innerHTML = "Hello7"; } else if (randNumForQuote == 8) { document.getElementById("quoteDiv").innerHTML = "Hello8"; } else if (randNumForQuote == 9) { document.getElementById("quoteDiv").innerHTML = "Hello9"; } else if (randNumForQuote == 10) { document.getElementById("quoteDiv").innerHTML = "Hello10"; } } </script> <div id="quoteDiv"></div> </body>


Although you ca reduce the code虽然你可以减少代码

 <body> <script> window.onload = function() { var randNumForQuote = Math.floor((Math.random() * 11)); document.getElementById("quoteDiv").innerHTML = "Hello" + (randNumForQuote ? " " + randNumForQuote : ''); } </script> <div id="quoteDiv"></div> </body>

Javascript needs to be in a script tag if it's in html, and you need to have the div come before the script tag. Javascript 需要在脚本标签中,如果它在 html 中,并且你需要让 div 出现在脚本标签之前。

 <body> <div id="quoteDiv"></div> <script> var randNumForQuote = Math.floor((Math.random() * 11)); if (randNumForQuote == 0) { document.getElementById("quoteDiv").innerHTML = "Hello"; } else if (randNumForQuote == 1) { document.getElementById("quoteDiv").innerHTML = "Hello1"; } else if (randNumForQuote == 2) { document.getElementById("quoteDiv").innerHTML = "Hello2"; } else if (randNumForQuote == 3) { document.getElementById("quoteDiv").innerHTML = "Hello3"; } else if (randNumForQuote == 4) { document.getElementById("quoteDiv").innerHTML = "Hello4"; } else if (randNumForQuote == 5) { document.getElementById("quoteDiv").innerHTML = "Hello5"; } else if (randNumForQuote == 6) { document.getElementById("quoteDiv").innerHTML = "Hello6"; } else if (randNumForQuote == 7) { document.getElementById("quoteDiv").innerHTML = "Hello7"; } else if (randNumForQuote == 8) { document.getElementById("quoteDiv").innerHTML = "Hello8"; } else if (randNumForQuote == 9) { document.getElementById("quoteDiv").innerHTML = "Hello9"; } else if (randNumForQuote == 10) { document.getElementById("quoteDiv").innerHTML = "Hello10"; } </script> </body>

<html>
<body> 
    <div id="quoteDiv"></div>



</body>
<script>
// self executing function here

   var randNumForQuote = Math.floor((Math.random() * 11));

  if (randNumForQuote == 0) {
    document.getElementById("quoteDiv").innerHTML = "Hello";
  } else if (randNumForQuote == 1) {
    document.getElementById("quoteDiv").innerHTML = "Hello1";
  } else if (randNumForQuote == 2) {
    document.getElementById("quoteDiv").innerHTML = "Hello2";
  } else if (randNumForQuote == 3) {
    document.getElementById("quoteDiv").innerHTML = "Hello3";
  } else if (randNumForQuote == 4) {
    document.getElementById("quoteDiv").innerHTML = "Hello4";
  } else if (randNumForQuote == 5) {
    document.getElementById("quoteDiv").innerHTML = "Hello5";
  } else if (randNumForQuote == 6) {
    document.getElementById("quoteDiv").innerHTML = "Hello6";
  } else if (randNumForQuote == 7) {
    document.getElementById("quoteDiv").innerHTML = "Hello7";
  } else if (randNumForQuote == 8) {
    document.getElementById("quoteDiv").innerHTML = "Hello8";
  } else if (randNumForQuote == 9) {
    document.getElementById("quoteDiv").innerHTML = "Hello9";
  } else if (randNumForQuote == 10) {
    document.getElementById("quoteDiv").innerHTML = "Hello10";
  }
</script>

</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Fastify 模式验证不起作用。 我是否以错误的方式配置了某些东西? - Fastify schema validation isn't working. Do I have something configured the wrong way? javascript if else 语句不起作用。 即使我单击 twoStory 图像,它也会返回 singleStory。 我试过了!=和--- - javascript if else statement not working. It returns singleStory even if I click on the twoStory image. I have tried != and --- CubeTextureLoader() 不工作。 或者我做错了什么 - CubeTextureLoader() not working. Or I'm doing something wrong ngRoute不工作。 有什么我做错了 - ngRoute not working. is there something i am doing wrong 我在 Javascript 控制台中有一个循环不起作用。 一种 - I have a loop in the Javascript console that is not working. A 为什么我在此JavaScript代码上输入了错误的输出? - Why I got wrong output in on this Javascript code? 我正在尝试运行脚本,但是我执行的步骤不起作用。 我哪里出问题了? - I am trying to run a script but the steps I have taken are not working. Where have I gone wrong? 我写的自定义 gulp 文件有问题,还是我必须添加其他东西? - Is there something wrong with the custom gulp-file I wrote, or do I have to add something else? 如果javascript中的else语句不起作用。 仅在else语句中 - if else statement in javascript is not working. only in else statement 如何使用for()或JavaScript中的其他代码缩短代码? - How can I shorten my code with for() or something else in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM