2017-05-25 87 views
1

對不起,我的格式很爛。每次單擊按鈕時我都會顯示一個隨機引號,每次單擊它時都會顯示一個不同的引號。我需要添加一個document.write或其他任何地方?我確定這裏和那裏有一堆錯誤,但我只想從函數中返回一個隨機引用。Javascript函數不顯示隨機消息

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Random quote</title> 

    <style> 

body { 
    background-color: #fff; 
    font-family: sans-serif; 
    color: #28315C; 
} 

h1 { 
    text-align: center; 
} 

button { 
    border-color: #000; 
    background-color: #88D0FE; 
    color: #000; 
    font-size: 20px; 



} 
</style> 
</head> 
<body> 
    <script> 

    var quotes = ['this is fortune 1', 
        'this is fortune 2', 
        'this is fortune 3', 
        'this is fortune 4', 
        'this is fortune 5', 
        'this is fortune 6', 
        'this is fortune 7', 
        ]; 


     function newQuote(){ 

     var randomNumber = Math.floor(math.random() * (quotes.length)); 

     document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber]; 


} 

    </script> 


</body> 

    <div id="quoteDisplay"> 
    </div> 
    <button onclick="newQuote()">New Quote</button> 


</html> 
+2

變化'Math.floor(的Math.random()''到Math.floor(的Math.random()' –

+1

調試步驟1 - 瀏覽器** **開發工具控制檯(F12) - 你的問題是'數學'!=='數學' –

回答

-1

第45行上鍵入數學與小寫M.改變這一行:

var randomNumber = Math.floor(math.random() * (quotes.length)); 

到:

var randomNumber = Math.floor(Math.random() * (quotes.length)); 
0

在第一,改變mathMath。此外,如果您只想設置文本內容,則可以使用innerText屬性而不是innerHTML。運行下面的代碼片段。

<script> 
 
    var quotes = ['this is fortune 1', 
 
    'this is fortune 2', 
 
    'this is fortune 3', 
 
    'this is fortune 4', 
 
    'this is fortune 5', 
 
    'this is fortune 6', 
 
    'this is fortune 7', 
 
    ]; 
 

 
    function newQuote() { 
 
    var randomNumber = Math.floor(Math.random() * (quotes.length)); 
 
    document.getElementById('quoteDisplay').innerText = quotes[randomNumber]; 
 
    } 
 
</script> 
 

 
<div id="quoteDisplay"> 
 
</div> 
 
<button onclick="newQuote()">New Quote</button>