2013-02-26 24 views
0

我有一個裝滿了引號的數組,並且在加載時顯示一個隨機引用。我遇到的問題是,當我只加載了2個引號時,一切正常,但是當我添加完整列表時,它不再運行。javascript數組僅在內容不完整時才起作用

這裏是代碼工程─

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

回答

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> 
+0

非常感謝! – user1570257 2013-02-26 03:52:58

1

JavaScript字符串中不能有換行符。你需要改變這一點:

"str part 
the rest" 

這樣:

"str part\nthe rest" 

"str part\n" 
+ "the rest" 

你應該檢查錯誤JavaScript控制檯;這可以幫助您調試

1

此外,你可以寫多成蔭的字符串是這樣的:

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

反斜槓之前斷行是另一種解決方案。

相關問題