简单的JS选择随机引用不起作用

时间:2017-03-26 16:20:05

标签: javascript html

我正在尝试使用下面的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>

1 个答案:

答案 0 :(得分: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();