我的报价不打印

时间:2016-08-10 17:57:38

标签: javascript html

我没有对其中一个功能进行参考,任何人都可以向我解释我缺少的东西吗?

到目前为止,我已经创建了一个引号数组,现在我正在尝试将它们与随机生成器一起使用。我点击按钮后想在页面上显示它们。 虽然我已经研究了一段时间,但却无法看到我在哪些方面对功能进行了参考。

这是我到目前为止所做的:

    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');
}

2 个答案:

答案 0 :(得分:1)

首先,你必须从这个数组中删除引用

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

因为在这里你要向数组添加字符串而不是数组。所以我把它改成了

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

我在迭代这个引号数组时提醒所有引号。 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>

答案 1 :(得分:0)

错误在这里:

&#13;
&#13;
    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());
&#13;
<div id="quote-box"></div>
&#13;
&#13;
&#13;

  • 您使用quotes[randomNumber]获得单个文档,因此无需循环,
  • 您没有将innerHTML设置为getRandomQuote()中的printQuote()值,
  • return没有getRandomQuote()值。