javascript随机报价生成器未打印
我试图在每次刷新时从列表中生成一个新的报价。出于某种原因,我无法在div中显示引号,我也不知道为什么 任何帮助都将不胜感激javascript随机报价生成器未打印,javascript,Javascript,我试图在每次刷新时从列表中生成一个新的报价。出于某种原因,我无法在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
<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>
var quotes=新数组();
quotes[0]=“Quote 1
作者”;
quotes[1]=“Quote 2
作者”;
var random=Math.ceil(Math.random()*quotes.length)-1;
$('quotes').set('html',quotes[random]);
多亏了大家的帮助,我更新了我的代码-
<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>
变量引号=[
“有些人感觉到下雨了,有些人只是被淋湿了。”
鲍勃·马利/b>“,
“不要祈求安逸的生活,祈祷有力量忍受艰难的生活。”
李小龙,
“成功不是最终的,失败不是致命的:重要的是继续下去的勇气。”
温斯顿
丘吉尔“,
“如果你的梦想没有吓到你,那么它们还不够大。”,
“成长并成为真正的自己需要勇气。”
E.E.卡明斯,
“所有的结局都是开始,我们只是还不知道而已。
”,
“有三种人:促成它发生的人,目睹它发生的人,以及
那些想知道到底发生了什么的人。
“,
“有些人很穷,他们只有钱。”,
“事情不会发生,事情是注定要发生的。”
约翰·F·肯尼迪,
“命运是一个名字,回顾过去,人们常常给那些产生戏剧性后果的选择起这个名字。”
J.K.罗琳,
“当我5岁的时候,我妈妈总是告诉我幸福是生活的关键。
当我上学时,他们问我长大后想成为什么样的人
他们告诉我我不懂作业,我也告诉他们不懂
了解生活。“
约翰·列侬”,
“并不是所有的流浪者都迷路了”
JRR托尔金,
“我们都会死。我们的目标不是长生不老,我们的目标是创造一些有意义的东西。”,
“追求进步,而不是完美。
”,
“定义我们的是我们跌倒后能站起来有多好。
”,
“一旦你知道自己的价值观是什么,就不难做出决定。”
Roy E.Disney,
“抱歉还不够好。
”,
“我可能还没到那里,但我比昨天更近了。
每一天都是一个新的开始。远离可能发生的事情,看看可能发生的事情
是。
“,
“谁给了你灵感?
”,
“如果你遵守规则的时间足够长,那么你可以改变游戏。”
Enders游戏”
];
document.getElementById('quotes').innerHTML=quotes[Math.floor(Math.random()*quotes.length)];
出于某种原因,div仍然在加载,没有考虑任何内容
是我引文里的引文把事情搞砸了。谢谢大家的帮助 EDIT:Nevermind,似乎是MooTools中的
$
确实应该返回id为引号的元素。如果您想在没有框架的情况下做同样的事情,可以使用document.getElementById('quotes').innerHTML=quotes[random]
您的$
正在尝试查找名为
的元素,而不是id为“quotes”的元素(假设它使用类似jQuery的CSS选择器)。使用$(“#引号”)
应该可以解决这个问题
还有几件事:可以用更简洁的方式编写数组:
quotes = [
"<i>Quote 1</i><br><b>Author</b>",
"<i>Quote 2</i><br><b>Author</b>"
];
quotes=[
“引用1作者”,
“引用2
作者”
];
另外,您可以只使用Math.floor
而不是Math.ceil
作为随机值,这样您就不需要在最后使用-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作者”,
“引用2
作者”
];
document.getElementById('quotes').innerHTML=
引号[Math.floor(Math.random()*quotes.length)];
语法看起来更像是MooTools,而不是jQuery.Oh。嗯,如果它使用的是像jQuery这样的CSS选择器,那么#引号应该可以起到任何作用。如果确实是MooTools,那么语法是正确的(如果内存可用,尽管我已经有一段时间没有编写任何MT代码了:)。是的,做出假设不是一个好主意。我自己网站的JS文件将$
定义为一个函数,它的运行就像它被放置在
标记之前一样,你不能仅仅假设它意味着jQuery。似乎MooTools'$确实应该返回id为“quotes”的div,而$$将是CSS选择器的简写形式,类似于jQuery的$。使用随机引号更新的脚本本身位于“quotes”div中,因此设置其html
将覆盖脚本。此外,由于它是一个内联脚本,它将在加载DOM时执行,并在DOM中“就绪”之前尝试更新html
(即,尚未到达结束div标记)。尝试将脚本移动到页面底部或框架(MooTools?)的dom就绪事件中。@Bryan这没关系,因为一旦脚本加载到内存中,您甚至可以删除它的
节点,它仍将运行。@Kolink,说得好,感谢您的更正。每次我看到有人使用新数组()
在JavaScript中,请告诉我一点。谢谢您的帮助!我试着用你的例子更新我的代码,但由于某些原因,我的div仍然加载为空