可以在一个外部 javascript 文件中添加多个函数吗?即.js文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14209105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can more than one function be added in an external javascript file? i.e .js file?
提问by suhas
I tried to write more that one function in an external .js file and it was not working. So i wrote the javascript inside the html body by using tag. cant we write more than 1 function outside? coz only the 1st function used to work well. Where as the rest did not work. I'm a fresher
我试图在外部 .js 文件中编写更多的函数,但它不起作用。所以我使用标签在 html 正文中编写了 javascript。我们不能在外面写超过 1 个函数吗?因为只有第一个功能可以很好地工作。其余的不起作用。我是新鲜人
回答by Nitesh Singh Rajput
Yes you can write multiple function into external java script file.i am posting here one small sample. 1. create test.js file inside one folder named as js 2. create test.html file
是的,您可以将多个函数写入外部 java 脚本文件。我在这里发布了一个小示例。1. 在一个名为 js 的文件夹中创建 test.js 文件 2. 创建 test.html 文件
1. test.html
1. 测试.html
insert following line into test.html file
将以下行插入 test.html 文件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/test.js"></script>
</head>
<body>
<a href="#" onclick="test()">Click Here to call first function</a><br>
<a href="#" onclick="test1()">Click Here to call second function</a>
</body>
</html>
- test.js
- 测试.js
Insert following code into your external java script file which name is test.js
将以下代码插入名为 test.js 的外部 java 脚本文件中
function test(){
alert("fist function is called");
}
function test1(){
alert("second function is called");
}