我有一个像这样的脚本:
<span>Famous Quote:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
var Quotation=new Array() // do not change this!
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.";
Quotation[10] = "Things are only impossible until they're not.";
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>
如何将引文放在自己的文件中,而不是源代码?以下是一个示例:http://mydomain.go/quote.js
答案 0 :(得分:4)
这会更好:
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.",
"Things are only impossible until they're not."
]
如果你真的想要,你可以把它放在一个单独的脚本中。除此之外,没有任何简单的&#39;改进剧本的方法。
编辑:
将其放在单独的文件中,然后添加脚本标记:
<script src="whatever.js"></script>
然后您就可以访问主文件中的Quotation
。
答案 1 :(得分:2)
那就是数组,你可以这样去
var Quotation=["Time is of the essence! Comb your hair.","Sanity is a golden apple with no shoelaces.",... and so on]
您的第二种方式可能是维护XML,这将使您可以自由地扩展您的报价列表。
创建一个类似于quot.xml的xml并通过js
访问它<QuotList>
<quotation>Time is of the essence! Comb your hair.</quotation>
<quotation>Sanity is a golden apple with no shoelaces</quotation>
</QuotList>
答案 2 :(得分:2)
创建一个文件,我们称之为quotes.js并添加:
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.",
"Things are only impossible until they're not."
];
不能通过
将此文件包含在主脚本中<script src="path/to/quotes.js"></script>
从这里做任何你想做的事情:
<script>
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>
注:
从html5起,您不再需要脚本标记中的javascript和text / javascript,因为它们现在是脚本标记的默认值。
注2:
你不应该在生产中使用document.write!
答案 3 :(得分:1)
在HTML文件中包含 quotes.js 文件。
HTML部分:
<!DOCTYPE html>
<html>
<head>
<script src="quote.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<span>Famous Quote:
<script language="JavaScript">
console.log(Quotation);
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>
</body>
</html>
注意:创建一个新文件 quote.js 并将此代码放在一个文件中,此文件已包含在HTML页面中。
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.",
"Things are only impossible until they're not."
]
答案 4 :(得分:1)
首先,简化数组声明:
var Q = [
"Time is of the essence! Comb your hair.",
"Sanity is a golden apple with no shoelaces."
];
然后,你的随机是不平等的。请改用Math.floor(Math.random()*Q)
;
document.write(Q[Math.floor(Math.random()*Q.length)]);
然后将数组声明移动到自己的js文件(quotes.js
)中,并将其包括在:
<script src="quotes.js"></script>
这就是全部:
<script src="quotes.js"></script>
document.write(Q[Math.floor(Math.random()*Q.length)]);
答案 5 :(得分:1)
这很棒!谢谢大家!
var quotes = [
"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.",
"Things are only impossible until they're not."
];
var randQuote = quotes[Math.floor(Math.random()*quotes.length)];
document.write(randQuote);
&#13;
答案 6 :(得分:0)
这是我将如何做代码...
var quotes = [
"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.",
"Things are only impossible until they're not."
];
var randQuote = quotes[Math.floor(Math.random()*quotes.length)];
document.write(randQuote);
答案 7 :(得分:0)
您可以使用模块加载程序将代码拆分为不同的文件。
index.html
内容:
<script data-main="scripts/main.js" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.15/require.js"></script>
scripts/main.js
内容:
require(['quotations'], function (quotations) {
var whichQuotation = Math.floor(Math.random() * quotations.length);
function showQuotation(){
document.write(quotations[whichQuotation]);
}
showQuotation();
});
scripts/quotations.js
内容:
define(function () {
return [
"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.",
"Things are only impossible until they're not."
];
});
JavaScript没有import
语句。在<script>
标记之间拆分代码很困难,因为像Quotation
这样的变量会成为全局变量。因此,如果在您的网站上工作的任何其他人试图定义一个名为Quotation
的变量,那么您就会被搞砸了。使用模块加载器可以共享引用列表而不使用该变量。
我还修复了你的随机索引算法。