如何在html文件中访问外部JavaScript数组? [英] How can you access external JavaScript arrays in an html file?
问题描述
我正在用不同的事实排列创建一个学校项目的网站。我用不同的JavaScript事实数组创建了多个文件,并试图在index.html文件中调用它们,但我不确定如何在没有href标记的情况下调用它们。我已经研究过,大多数人都说要尝试在标签中采购,但没有任何印刷。
这是我的index.html代码:
< ;! doctype html>
< html>
< head>
< meta charset =utf8/>
< title>< / title>
< link rel =stylesheettype =text / csshref =main.css/>
< script src =EllaFactsArray.htmltype =text / javascript>< / script>
< / head>
< body>
< p>
这是关于Ella的事实:
< / p>
< script>
getEllaFact();
< / script>
< / body>
< / html>
这是我的事实数组(EllaFactsArray.html):
< html>
< title> Ella Facts< / title>
< head>
< script type =text / javascript>
函数getEllaFact()
{
Ella = new Array(6);
Ella [0] =Ella有六十年的职业生涯。;
Ella [1] =;
Ella [2] =;
Ella [3] =;
Ella [4] =;
Ella [5] =;
i = Math.floor(Math.random()* Ella.length);
document.write(< dt>+ Ella [i] +\\\
);
}
< / script>
< / head>
< body>
< script type =text / javascript>
getEllaFact();
< / script>
< / body>
< / html>
脚本需要引用JavaScript文件,而不是包含函数的HTML文件。
< script src =EllaFactsArray.js类型= 文本/ JavaScript的 >< /脚本> b
$ b https://www.w3schools.com/js/DEFAULT.asp
I'm creating a website for a school project with different arrays of facts. I created multiple files with different JavaScript facts arrays and am trying to call them in the index.html file but I'm not sure how to call them without an a href tag. I've researched and most people say to try sourcing in the tag but nothing is printing.
This is my index.html code:
<!doctype html>
<html>
<head>
<meta charset="utf8" />
<title></title>
<link rel="stylesheet" type="text/css" href="main.css" />
<script src="EllaFactsArray.html" type="text/javascript"></script>
</head>
<body>
<p>
Here is a fact about Ella:
</p>
<script>
getEllaFact();
</script>
</body>
</html>
This is my facts array (EllaFactsArray.html):
<html>
<title>Ella Facts</title>
<head>
<script type="text/javascript">
function getEllaFact()
{
Ella = new Array(6);
Ella[0] = "Ella had a six decade long career.";
Ella[1] = "";
Ella[2] = "";
Ella[3] = "";
Ella[4] = "";
Ella[5] = "";
i = Math.floor(Math.random() * Ella.length);
document.write("<dt>" + Ella[i] + "\n");
}
</script>
</head>
<body>
<script type="text/javascript">
getEllaFact();
</script>
</body>
</html>
解决方案 The script needs to reference a JavaScript file, not an HTML file containing a function. Move the function into its own file and include it in the pages as needed.
<script src="EllaFactsArray.js" type="text/javascript"></script>
https://www.w3schools.com/js/DEFAULT.asp
这篇关于如何在html文件中访问外部JavaScript数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!