随机报价发生器困境

时间:2016-11-21 03:44:13

标签: javascript

我的代码在Codepen上工作正常...... Random Quote Generator at Codepen

但是...

我一直在我的网站上讨论这个问题,但我只是没看到问题所在。 Random QuoteGenerator at my personal site

var quote = document.getElementById('quote');
var click = document.getElementById('click');
var quoteList = ["'You will never be happy if you continue to search for what happiness consists of. You will never live if you are looking for the meaning of life.'", 
  "'Do not read, as children do, to amuse yourself, or like the ambitious, for the purpose of instruction. No, read in order to live.'", 
  "'Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.'", 
  "'A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing.'", 
  "'In vain have I struggled. It will not do. My feelings will not be repressed. You must allow me to tell you how ardently I admire and love you.'",
  "'It is better to remain silent at the risk of being thought a fool, than to talk and remove all doubt of it.'",
  "'Have you ever been in love? Horrible isn't it? It makes you so vulnerable. It opens your chest and it opens up your heart and it means that someone can get inside you and mess you up.'",
  "'The problem with the world is that the intelligent people are full of doubts, while the stupid ones are full of confidence.'",
  "'Ok. You fuck me, then snub me. You love me, you hate me. You show me a sensitive side, then you turn into a total asshole. Is this a pretty accurate description of our relationship.'",
  "'I love sleep. My life has the tendency to fall apart when I'm awake, you know?.'",
  "'If I had a flower for every time I thought of you...I could walk through my garden forever.'",
  "'Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great.'",
  "'Never tell the truth to people who are not worthy of it.'",
  "'The one you love and the one who loves you are never, ever the same person.'",
  "'Becoming fearless isn't the point. That's impossible. It's learning how to control your fear, and how to be free from it.'",
  "'I hope she'll be a fool -- that's the best thing a girl can be in this world, a beautiful little fool.'"];


var randomQuote = function(){
  var twitter = document.getElementById('twitter');
  var index = Math.floor(Math.random()*quoteList.length);
  twitter.href = "https://twitter.com/intent/tweet?text=" + encodeURI(quoteList[index]);
  quote.innerHTML = quoteList[index];
};

randomQuote(); 


click.addEventListener('click', randomQuote, false);

所以问题是,发生了什么?相同的代码,报价不适用于我的个人网站。任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:1)

在您的HTML脚本元素中,您应该使用href属性引用脚本src

此外,您的代码在DOM存在之前执行,将Javascript移动到HTML正文部分的末尾。

    <script src="//six03.com/quotes/script.js" type="text/javascript"></script>
</body>
</html>

答案 1 :(得分:0)

您尝试在页面完成加载之前绑定事件。最简单的解决方法是将脚本标记移动到页面底部并将其加载到正文的末尾。

否则,您可以使用库来确保页面已加载或绑定到事件。在jQuery中将使用$(document).ready(function(){})事件。