2017-03-26 85 views
-2

我想實現一個簡單的JS隨機報價生成器使用下面的HTML和JS代碼。但是,它不返回任何引號。我是JS的新手。感謝任何幫助。簡單的JS來選擇隨機報價不工作

HTML

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
<script src="https://abcd.com/wp-includes/js/quotes.js"></script> 
<script> 
showQuotation(); 
</script> 
</body> 
</html> 

JS (這是https://abcd.com/wp-includes/js/quotes.js代碼)

<script type="text/javascript"> 
// Copyright 2004 by CodeLifter.com 
var Quotation=new Array() 
Quotation[0] = "Time is of the essence! Comb your hair."; 
Quotation[1] = "Sanity is a golden apple with no shoelaces."; 
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon."; 
Quotation[3] = "Honesty blurts where deception sneezes."; 
Quotation[4] = "Pastry satisfies where art is unavailable."; 
Quotation[5] = "Delete not, lest you, too, be deleted."; 
Quotation[6] = "O! Youth! What a pain in the backside."; 
Quotation[7] = "Wishes are like goldfish with propellors."; 
Quotation[8] = "Love the river's \"beauty\", but live on a hill."; 
Quotation[9] = "Invention is the mother of too many useless toys."; 
var Q = Quotation.length; 
var whichQuotation=Math.round(Math.random()*(Q-1)); 
function showQuotation(){document.write(Quotation[whichQuotation]);} 
showQuotation(); 
</script> 
+3

JS文件不應該包含''和''標籤,這些都是嵌入JS在HTML中。 – jcaron

+2

在進行Web開發時,必須保持開發工具處於打開狀態並在Web控制檯中查找錯誤,逐步瀏覽內置調試器中的代碼等。 –

+2

還要注意,您顯示報價兩次, quotes.js'(最後),以及之後的內聯腳本。 –

回答

0

按照所有提出的意見,我已經如下修改我的代碼。讓我知道它的技術上是否正確。從需求角度來看,它按預期工作。不知道是否有正確的方式來傳達它(通過發佈答案)。如果沒有,請讓我知道。

HTML

<p id="quotes-div"></p> 
<script src="https://abcd.com/wp-includes/js/quotes.js"> 
</script> 

JS 代碼中quotes.js

// Copyright 2004 by CodeLifter.com 
var Quotation= 
[ 
    "Time is of the essence! Comb your hair.", 
    "Sanity is a golden apple with no shoelaces.", 
    "Repent! The end is coming, $9.95 at Amazon.", 
    "Honesty blurts where deception sneezes.", 
    "Pastry satisfies where art is unavailable.", 
    "Delete not, lest you, too, be deleted.", 
    "O! Youth! What a pain in the backside.", 
    "Wishes are like goldfish with propellors.", 
    "Love the river's \"beauty\", but live on a hill.", 
    "Invention is the mother of too many useless toys.", 
]; 
function showQuotation() 
{ 
    document.getElementById("quotes-div").innerHTML = Quotation[Math.floor(Math.random() * Quotation.length)] 
} 
showQuotation(); 
+0

我編輯了你的答案來改變引用定義的縮進,它應該使事情更容易閱讀。 – jcaron