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].

ID「引用符」を持つ要素ではなく、という$要素を見つけようとしています(jQueryのようなCSSセレクターを使用していると仮定します)。<quotes>を使用$('#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 に答える