0

我试图在每次刷新时从列表中生成新的报价。出于某种原因,我无法在 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.”&lt;/i><br><b>Bruce Lee</b>",
"<i>“Success is not final, failure is not fatal: it is the courage to continue that counts.”&lt;/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.”&lt;/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.”&lt;/i><br><b>John F. Kennedy</b>",
"<i>“Destiny is a name often given in retrospect to choices that had dramatic consequences.”&lt;/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.”&lt;/i><br><b>John Lennon</b>",
"<i>“Not all those who wander are lost”&lt;/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.”&lt;/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.”&lt;/i><br><b>Enders Game</b>"
];

document.getElementById('quotes').innerHTML = quotes[Math.floor(Math.random() * quotes.length)];
</script>

出于某种原因,div 仍在加载而没有考虑任何内容。

是我的报价里面的报价搞砸了。谢谢大家的帮助!!

4

2 回答 2

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>
于 2013-02-26T01:43:08.617 回答
0

编辑:没关系,在 MooTools 中似乎$确实应该返回带有 id 的元素quotes。如果你想在没有框架的情况下做同样的事情,你可以使用document.getElementById('quotes').innerHTML = quotes[random].

$正在尝试查找名为 的元素<quotes>,而不是 ID 为“quotes”的元素(假设它使用像 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

于 2013-02-26T01:34:23.107 回答