单词不会按下按钮[JS,HTML]

时间:2016-11-17 18:12:17

标签: javascript html

我很困惑为什么在按下按钮时它没有输出一个单词。如果有人能帮我解决这个问题,那将非常感激。我试过在其他地方搜索它,但我不知道这是否是我唯一的问题(不太可能)或者我是不是很擅长搜索这样的东西谷歌。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title> Seth's Pictionary Word Generator </title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script>
      function numGen() {
        num = return Math.floor((Math.random() * 43) + 1)
        window.num = nNum;
    }

      var eWords = ["cat", "sun", "cup", "ghost", "flower", "pie", "cow", "banana", "snowflake", "bug", "book", "jar", "snake", "light", "tree", "lips", "apple", "slide", "socks", "smile", "swing", "coat", "shoe", "water", "heart", "hat", "ocean", "kite", "dog", "mouth", "milk", "duck", "eyes", "skateboard", "bird", "boy", "apple", "person", "girl", "mouse", "ball", "house", "star", "nose", "bed", "whale", "jacket", "shirt", "hippo", "beach", "egg", "face", "cookie", "cheese", "ice cream code", "drum", "circle", "spoon", "worm", "spider web"];



      function write() {
        var word = eWords[nNum]
        document.getElementById("wordPlace").innerHTML = word;
  }

    </script>
  </head>
  <body>

    <h1 align="center" class="head">SETH'S PICTIONARY GENERATOR</h1>

    <p align="center" class=b> Home of the <strong> best </strong> pictionary generator on the web.</p>

    &nbsp;
    &nbsp;
    &nbsp;


    <div id="wordPlace">

    </div>
    &nbsp;
    &nbsp;
    &nbsp;
    <p align="center">
      <button class="btn btn-primary" onclick="numGen() write()">
        Generate a word!
      </button>
    </p>

    <footer class=footer>

      <div align="center" class=foot-div>
        Created by gp_swade -- github.com/swodobaggins/pictionary-generator
      </div>

    </footer>
  </body>

</html>

1 个答案:

答案 0 :(得分:0)

我修改后的工作代码:(由于某些原因,jsfiddle和类似网站不起作用,但在localhost上它完美运行)

更新:使用jsfiddle(只需将JS“加载类型”更改为“No wrap-in&lt; head&gt;”): https://jsfiddle.net/gy2ofu26/

...
...
<script>
    var nNum;

    function numGen() {
        num = Math.floor((Math.random() * 43) + 1);
        nNum = num;
    }

    var eWords = ["cat", "sun", "cup", "ghost", "flower", "pie", "cow", "banana", "snowflake", "bug", "book", "jar", "snake", "light", "tree", "lips", "apple", "slide", "socks", "smile", "swing", "coat", "shoe", "water", "heart", "hat", "ocean", "kite", "dog", "mouth", "milk", "duck", "eyes", "skateboard", "bird", "boy", "apple", "person", "girl", "mouse", "ball", "house", "star", "nose", "bed", "whale", "jacket", "shirt", "hippo", "beach", "egg", "face", "cookie", "cheese", "ice cream code", "drum", "circle", "spoon", "worm", "spider web"];

    function write(){
        var word = eWords[nNum];
        document.getElementById("wordPlace").innerHTML = word;
    }

    function callFunc(){ numGen(); write(); }
</script>
...
...
<button class="btn btn-primary" onclick="callFunc();">