2016-08-10 8 views
-5

私は関数のいずれかで参照をしていません、誰かが私に行方不明を説明することができますか?

これまでは、引用符の配列を作成しましたが、今はそれらをランダムジェネレータで使用しようとしています。ボタンをクリックすると、それらをページに表示したいのですが。 私はしばらくそれに取り組んでいますが、私がどこで機能についての参照をしているのかわかりません。ここで私の引用符は印刷されていません

は、私がこれまで持っているものです。


var quotes = ['quoteOne', 'quoteTwo', 'quoteThree', 'quoteFour',  'quoteFive']; 
quoteOne = { 
    quote : 'I meant what I said and I said what I meant.', 
    source : 'Dr. Seuss', 
    citation : 'dont know', 
    year : '1990' 
} 
    quoteTwo = { 
    quote : 'Truth is everybody is going to hurt you: you just gotta find the  ones worth suffering for.', 
    source : 'Bob Marley', 
    citation : 'Smoke session', 
    year : '1989' 
} 
    quoteThree = { 
    quote : 'Never interrupt your enemy when he is making a mistake.', 
    source : 'Napoleon Bonaparte', 
    citation : 'I dont know', 
    year : 'N/a' 
} 
    quoteFour = { 
    quote : 'Fear is the main source of superstition, and one of the main sources of cruelty. To conquer fear is the beginning of wisdom.', 
    source : 'Bertrand Russell', 
    citation : 'I dont know', 
    year : 'N/a' 
} 
    quoteFive = { 
    quote : 'Curiosity will conquer fear even more than bravery will.', 
    source : 'James Stephens', 
    citation : 'I dont know', 
    year : 'some year' 
} 
function getRandomQuote() { 
    var item = quotes[Math.floor(Math.random()* quotes.length)]; 
    for(var i=0; i<quotes.length; i++){ 
    var quotes = quotes[i]; 
    var content = '<strong class="lead" style="color:#00000;">' + quotes.name + '</strong> - (<a target="_blank" href="' + quotes.source + '">url</a>)<hr style="margin:0;"><p style="margin-bottom:0;">' + quotes.citation + '<br>' + quotes.year + ', ' ; 
     document.getElementById('loadQuote').addEventListener("click",  printQuote, false); 
    } 
    console.log('im working'); 
} 
    function printQuote() { 
    var print = getRandomQuote(); 
    document.getElementById('quote-box').innerHTML 
    console.log('im still working'); 
} 
+0

スニペット? – David

答えて

0

エラーはここにある:

quoteOne = { 
 
     quote : 'I meant what I said and I said what I meant.', 
 
     source : 'Dr. Seuss', 
 
     citation : 'dont know', 
 
     year : '1990' 
 
    } 
 
     quoteTwo = { 
 
     quote : 'Truth is everybody is going to hurt you: you just gotta find the  ones worth suffering for.', 
 
     source : 'Bob Marley', 
 
     citation : 'Smoke session', 
 
     year : '1989' 
 
    } 
 
     quoteThree = { 
 
     quote : 'Never interrupt your enemy when he is making a mistake.', 
 
     source : 'Napoleon Bonaparte', 
 
     citation : 'I dont know', 
 
     year : 'N/a' 
 
    } 
 
     quoteFour = { 
 
     quote : 'Fear is the main source of superstition, and one of the main sources of cruelty. To conquer fear is the beginning of wisdom.', 
 
     source : 'Bertrand Russell', 
 
     citation : 'I dont know', 
 
     year : 'N/a' 
 
    } 
 
    quoteFive = { 
 
     quote : 'Curiosity will conquer fear even more than bravery will.', 
 
     source : 'James Stephens', 
 
     citation : 'I dont know', 
 
     year : 'some year' 
 
    } 
 
    var quotes = [quoteOne, quoteTwo, quoteThree, quoteFour,quoteFive]; 
 

 
    function getRandomQuote() { 
 
     var item = quotes[Math.floor(Math.random()* quotes.length)]; 
 
     return '<strong class="lead" style="color:#00000;">' + item.quote + '</strong> - (<a target="_blank" href="' + item.source + '">url</a>)<hr style="margin:0;"><p style="margin-bottom:0;">' + item.citation + '<br>' + item.year + ', ' ; 
 
    } 
 
    function printQuote() { 
 
     document.getElementById('quote-box').innerHTML = getRandomQuote(); 
 
    } 
 

 
    console.log(printQuote());
<div id="quote-box"></div>

  • あなたはquotes[randomNumber]を使用して単一のドキュメントを取得し、そのループの必要性、
  • 何あなたはgetRandomQuote()からのreturn値はありませんでした
  • printQuote()getRandomQuote()値にinnerHTMLを設定していませんでした。
+0

今、それは私に未定義の、素晴らしいことはよく働いてくれています。 –

+0

「クリック」アクションが追加されました。時計のように機能しています。ありがとうございました。 –

+0

それは大丈夫です。それが助けられたら、答えを受け入れてください! –

1

あなたがここにあなたが配列していない配列に文字列を追加しているので、この配列

var quotes = ['quoteOne', 'quoteTwo', 'quoteThree', 'quoteFour','quoteFive']; 

からの引用を削除する必要があり、すべての最初の。だから私はこの繰り返し処理する配列を引用しながら、すべての引用符を警告する

var quotes = [quoteOne, quoteTwo, quoteThree, quoteFour,quoteFive]; 

とIMにそれを変更しました。あなたが/どのように具体的には、失敗しないデバッガ、このステップスルーするとChechは

quoteOne = { 
 
    quote : 'I meant what I said and I said what I meant.', 
 
    source : 'Dr. Seuss', 
 
    citation : 'dont know', 
 
    year : '1990' 
 
} 
 
    quoteTwo = { 
 
    quote : 'Truth is everybody is going to hurt you: you just gotta find the  ones worth suffering for.', 
 
    source : 'Bob Marley', 
 
    citation : 'Smoke session', 
 
    year : '1989' 
 
} 
 
    quoteThree = { 
 
    quote : 'Never interrupt your enemy when he is making a mistake.', 
 
    source : 'Napoleon Bonaparte', 
 
    citation : 'I dont know', 
 
    year : 'N/a' 
 
} 
 
    quoteFour = { 
 
    quote : 'Fear is the main source of superstition, and one of the main sources of cruelty. To conquer fear is the beginning of wisdom.', 
 
    source : 'Bertrand Russell', 
 
    citation : 'I dont know', 
 
    year : 'N/a' 
 
} 
 
    quoteFive = { 
 
    quote : 'Curiosity will conquer fear even more than bravery will.', 
 
    source : 'James Stephens', 
 
    citation : 'I dont know', 
 
    year : 'some year' 
 
} 
 
    var quotes = [quoteOne, quoteTwo, quoteThree, quoteFour,quoteFive]; 
 
function getRandomQuote() { 
 
    var item = quotes[Math.floor(Math.random()* quotes.length)]; 
 
    for(var i=0; i<quotes.length; i++){ 
 
    var content = '<strong class="lead" style="color:#00000;">' + quotes[i].name + '</strong> - (<a target="_blank" href="' + quotes[i].source + '">url</a>)<hr style="margin:0;"><p style="margin-bottom:0;">' + quotes[i].citation + '<br>' + quotes[i].year + ', ' ; 
 
     alert(content); 
 
     document.getElementById('loadQuote').addEventListener("click",  printQuote, false); 
 
    } 
 
    alert('im working'); 
 
} 
 
    function printQuote() { 
 
    var print = getRandomQuote(); 
 
    document.getElementById('quote-box').innerHTML 
 
    console.log('im still working'); 
 
} 
 
printQuote();
<div id ='loadQuote'> khk</div> 
 
<div id = 'quote-box'> </div>

関連する問題