[英]Calling a javascript function in another javascript file just work in mozilla
我有两个JavaScript文件:
func1.js:
function HelloWorld() {
alert("hi");
}
func2.js:
function btnclick(){
$.getScript("func1.js", function () {
HelloWorld();
});
}
我的HTML代码是:
<html>
<head>
<script language="javasvript" type="text/javascript" src="jquery.js"></script>
<script language="javasvript" type="text/javascript" src="func2.js"></script>
</head>
<body>
<input type="button" onclick="btnclick()" value="Click me" />
</body>
</html>
当我单击按钮时,调用btnclick()
函数,并在btnclick
()函数中加载func1.js
并调用Helloworld()
函数。
但这仅在mozila上还可以。 不要在Chrome和IE中使用。 我该如何解决?
除了您没有提供足够的细节外,您还应该考虑修复代码中的错字。
我认为,如果您的代码可以在mozzila中运行,而不能在chrome中运行,那是因为您试图离线运行它。
Mozzila可以使ajax请求脱机,而chrome则不能,因此,在解决所有错字之后,请在服务器上运行该请求,以重试。
您只有一些语法错误:
<html>
<head>
<script type="text/javascript" src="func1.js"></script>
<script <!-- this does not exist: language="javasvript"--> type="text/javascript" src="func2.js"></script>
</head>
<body>
<input type="button" onclick="btnclick()" value="Click me" />
</body>
</html>
而且您不需要使用$ .getScript,只需调用以下函数:
function btnclick(){
HelloWorld();
}
由于该函数是全局函数,因此要从script2调用script1中的函数,唯一的要求是在script2之前加载script1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.