2013-04-06 63 views
-1

我是一個絕對的初學者,我試圖在任何瀏覽器(IE,FF,Chrome)中運行此JavaScript代碼。我使用Firebug作爲調試器,但沒有錯誤報告......我感謝你們所有的時間。JavaScript不會顯示在任何瀏覽器

function getQuote() { 
    // Create the arrays 
    quotes = new Arrays(4); 
    sources = new Arrays(4); 

    // Initialize the arrays with quotes 
    quotes[0] = "When I was a boy of 14, my father was so " + "ignorant...but when I got to be 21, I was astonished " + "at how much he had learned in 7 years."; 
    sources[0] = "Mark Twain"; 
    quotes[1] = "Everybody is ignorant. Only on different " + "subjects."; 
    sources[1] = "Will Rogers"; 
    quotes[2] = "They say such nice things about people at " + "their funerals that it makes me sad that I'm going to " + "miss mine by just a few days."; 
    sources[2] = "Garrison Keilor"; 
    quotes[3] = "What's another word for thesaurus?"; 
    sources[3] = "Steven Wright"; 

    // Get a random index into the arrays 
    i = Math.floor(Math.random() * quotes.length); 

    // Write out the quote as HTML 
    document.write("<dl style='background-color: lightpink'>\n"); 
    document.write("<dt>" + "\"<em>" + quotes[i] + "</em>\"\n"); 
    document.write("<dd>" + "- " + sources[i] + "\n"); 
    document.write("<dl>\n"); 
} 
+5

getQuote從哪裏調用? – smerny 2013-04-06 22:46:25

+0

如果您不調用該函數,它將不會執行任何操作。 – 2013-04-06 22:46:45

+0

除了使用'Arrays'而不是'Array'之外,除非您提供更多信息,否則無法確切地說出您的問題。請更新您的問題以提供問題的完整圖片。 – 2013-04-06 22:57:35

回答

0

我做了一些修改以獲得更好的性能。你只需要調用這個函數。

function getQuote() { 
     // Create the arrays 
     quotes = [ 
      { 
        quote: "When I was a boy of 14, my father was so ignorant...but when I got to be 21, I was astonished at how much he had learned in 7 years.", 
        source: "Mark Twain" 
      }, 
      { 
        quote: "Everybody is ignorant. Only on different subjects.", 
        source: "Will Rogers" 
      }, 
      { 
        quote: "They say such nice things about people at their funerals that it makes me sad that I'm going to miss mine by just a few days.",  
        source: "Garrison Keilor" 
      }, 
      { 
        quote: "What's another word for thesaurus?", 
        source: "Steven Wright" 
      } 
     ]; 

     // Get a random index into the arrays 
     i = ~~(Math.random() * quotes.length); 

     // Get body tag 
     var body = document.getElementByTagName("body")[0], 
      // Making a string to append to body 
      htmlCode = "<dl style='background-color: lightpink'>\n" + 
        "<dt>\"<em>" + quotes[i].quote + "</em>\"\n" + 
        "<dd>- " + quotes[i].source + "\n</dl>"; 

     // appending htmlCode to body 
     body.innerHTML += htmlCode; 

    } 

我創建對象,以便更好地組織陣列,因爲報價和源相關數據..

我收集有關最佳實踐在JavaScript的一些文章,一起來看看:

希望它可以幫助...

+0

對於初學者來說,'~~'按位操作會非常混亂。國際海事組織,堅持'Math.floor()'的清晰度。 – 2013-04-06 23:04:14

+1

這裏沒有性能瓶頸。堅持清晰,尤其是因爲他們還在學習。 – icktoofay 2013-04-06 23:10:15

+0

好吧,~~令人困惑.. 但它與DOM和數組的工作方式更快.. – fernandosavio 2013-04-07 00:24:49

1

首先,你要糾正這一點,

// Create the arrays 
quotes = new Arrays (4); 
sources = new Arrays (4); 

要這樣:

// Create the arrays 
quotes = new Array (4); 
sources = new Array (4); 

此外,您還需要調用頁面加載這一功能。將此行添加到腳本末尾:

window.onload = getQuote; 
+1

'window.onload = getQuote();'應該'window.onload = getQuote;' – 2013-04-06 23:03:16

+0

@amnotiam謝謝,更正那。 – 2013-04-06 23:06:28

相關問題