0

引用符でいっぱいの配列があり、読み込み時にランダムな引用符が表示されます。私が抱えている問題は、2 つの引用符しかロードされていない場合はすべて正常に動作することですが、リスト全体を追加すると実行されなくなります。

これが機能するコードです-

<div id="quotes">Quotes</div>


<script type="text/javascript">

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>"

];

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

上記は期待どおりに機能しますが、残りの引用符を追加すると (以下に示すように)、スクリプトが機能しなくなります。

<div id="quotes"></div>

<script type="text/javascript">
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>
4

3 に答える 3

1

JavaScript 文字列に改行を含めることはできません。これを変更する必要があります:

"str part
 the rest"

これに:

"str part\nthe rest"

また

"str part\n"
+ "the rest"

JavaScript コンソールでエラーを確認する必要があります。これはデバッグに役立ちます

于 2013-02-26T03:22:49.310 に答える
1

また、次のように複数行の文字列を記述することもできます。

var str = "beginning \
           continue \
           end.";

改行の前のバックスラッシュは、別の解決策です。

于 2013-02-26T03:36:00.887 に答える
0

文字列内の改行が問題の原因でした。ここに完全に機能するコードがあります: http://jsfiddle.net/whizkid747/5tyrY/

<div id="quotes">Quotes</div>


<script type="text/javascript">

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>WinstonChurchill</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>
于 2013-02-26T03:28:56.243 に答える