2016-11-17 41 views
0

我很困惑,爲什麼在按鈕上按它不輸出單詞。如果有人能幫我解決這個問題,那將不勝感激。我試圖在其他地方搜索它,但我不知道這是否是唯一的問題,只有我(不太可能),或者如果我不善於在Google上搜索這樣的內容。單詞不會打印在按鈕上按[JS,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> 
+0

你的變量不匹配。你有一個叫num,然後你試圖把它稱爲mNum或類似的東西..仔細檢查你的代碼,並確保你的var名稱是一致的。 –

+0

this:'window.num = nNum;'應該是這樣的:'window.nNum = num;' –

+0

'num'已經是'numGen'中的一個全局變量,因爲你沒有在它前面使用'var',所以你現在有'window.num'和'window.nNum' ...並且'onclick'處理程序需要在這兩個函數調用之間用分號分隔。 –

回答

0

我的修改,工作代碼:(出於某種原因的jsfiddle和類似的網站不工作,但在本地主機上它完美)

更新:對的jsfiddle工作(只是改變JS「負載型」到「不循環 - 在<頭>」): 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();"> 
+0

感謝您的支持!非常感謝。 – Seth

相關問題