2017-05-25 5 views
1

Sorry meine Formatierung saugt. Ich möchte, dass jedes Mal, wenn ich auf den Knopf klicke, ein zufälliges Zitat angezeigt wird und jedes Mal, wenn ich darauf klicke, ein anderes Zitat erscheint. Muss ich ein document.write oder ähnliches irgendwo hinzufügen? Ich bin mir sicher, dass es hier und da einige Fehler gibt, aber ich möchte nur ein zufälliges Zitat von der Funktion zurückgeben.Javascript Funktion zeigt keine zufällige Nachricht

<!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

Änderung 'Math.floor (math.random()' auf 'Math.floor (Math.random()' –

+1

Schritt 1 bei der Fehlersuche - Browser ** ** Entwickler-Tools-Konsole (F12) - Dein Problem ist das 'math'! ==' Math' –

Antwort

-1

On 45 Zeile eingegeben Sie Math mit einem kleinen M. Ändern Sie diese Zeile:

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

zu:

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

In ersten, math zu Math ändern. Wenn Sie nur Textinhalt festlegen möchten, können Sie auch die innerText Eigenschaft anstelle von innerHTML verwenden. Führen Sie das folgende Snippet aus.

<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>

Verwandte Themen