我正试图在每次刷新时从列表中生成一个新的引用。出于某种原因,我无法在div中显示引号,我不确定原因。
任何帮助将不胜感激!
<div id="quotes">
<script>
var quotes = new Array();
quotes[0] = "<i>Quote 1</i><br><b>Author</b>";
quotes[1] = "<i>Quote 2</i><br><b>Author</b>";
var random = Math.ceil (Math.random() * quotes.length) - 1;
$('quotes').set('html', quotes[random]);
</script>
</div>
我已经更新了我的代码,感谢大家对此的帮助 -
<div id="quotes"></div>
<script>
var quotes = [
"<i>"Some people feel the rain. Others just get wet."</i><br><b>Bob Marley/b>",
"<i>“Do not pray for an easy life, pray for the strength to endure a difficult one.”</i><br><b>Bruce Lee</b>",
"<i>“Success is not final, failure is not fatal: it is the courage to continue that counts.”</i><br><b>Winston
Churchill</b>",
"<i>If your dreams don’t scare you they’re not big enough.</i><br><b></b>",
"<i>“It takes courage to grow up and become who you really are.”</i><br><b>E.E. Cummings</b>",
"<i>All endings are also beginnings, we just don’t know it yet.</i><br><b></b>",
"<i>There are three kinds of people: Those who make it happen, those who watch it happen, and
those who wonder what the heck happened.</i><br><b></b>",
"<i>There are people so poor, that the only thing they have is money.</i><br><b></b>",
"<i>“Things do not happen. Things are made to happen.”</i><br><b>John F. Kennedy</b>",
"<i>“Destiny is a name often given in retrospect to choices that had dramatic consequences.”</i><br><b>J.K. Rowling</b>",
"<i>“When I was 5 years old, my mother always told me that happiness was the key to life.
When I went to school, they asked me what I wanted to be when I grew up. I wrote
down ‘happy’. They told me I didn’t understand the assignment, and I told them they didn’t
understand life.”</i><br><b>John Lennon</b>",
"<i>“Not all those who wander are lost”</i><br><b>JRR Tolkien</b>",
"<i>We all die. The goal isn’t to live forever, the goal is to create something that will.</i><br><b></b>",
"<i>Strive for progress, not perfection.</i><br><b></b>",
"<i>What defines us is how well we rise after falling.</i><br><b></b>",
"<i>“It’s not hard to make decisions once you know what your values are.”</i><br><b>Roy E. Disney</b>",
"<i>Sorry’s not good enough.</i><br><b></b>",
"<i>I may not be there yet, but I’m closer than I was yesterday.
Every day is a new beginning. Stay away from what might have been and look at what can
be.</i><br><b></b>",
"<i>Who inspires you?</i><br><b></b>",
"<i>“If you play by the rules long enough, then you can change the game.”</i><br><b>Enders Game</b>"
];
document.getElementById('quotes').innerHTML = quotes[Math.floor(Math.random() * quotes.length)];
</script>
出于某种原因,div仍在加载,没有任何内容想法。
这是我的报价内部的引用搞砸了。谢谢大家的帮助!!
答案 0 :(得分:1)
不再需要任何库,这使得代码变得更加容易。
<div id="quotes"></div>
<script>
var quotes = [
"<i>Quote 1</i><br><b>Author</b>",
"<i>Quote 2</i><br><b>Author</b>"
];
document.getElementById('quotes').innerHTML =
quotes[Math.floor(Math.random() * quotes.length)];
</script>
答案 1 :(得分:0)
编辑:没关系,似乎MooTools中的$
确实应该返回标识为quotes
的元素。如果你想在没有框架的情况下做同样的事情,可以使用document.getElementById('quotes').innerHTML = quotes[random]
。
您的$
正在尝试查找名为<quotes>
的元素,而不是ID为“引号”的元素(假设它使用了像jQuery这样的CSS选择器)。使用$('#quotes')
应该修复它。
其他一些事情:你可以用更简洁的方式编写数组:
quotes = [
"<i>Quote 1</i><br><b>Author</b>",
"<i>Quote 2</i><br><b>Author</b>"
];
此外,您可以使用Math.floor
代替Math.ceil
作为随机值,然后最后不需要- 1
。