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 回答