这有效...
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">
<script type="text/javascript" src="src/myJS.js"></script>
</head>
<body onload="myJS();">
</body>
</html>
外部 javascript 文件的内容(为方便起见称为 myJS.js)...
myJS = function ()
{
document.write("Hello world");
};
但是,这行不通......
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">
<script type="text/javascript" src="src/myJS.js"></script>
</head>
<body onload="myJS.myFunction();">
</body>
</html>
外部 javascript 文件 ...
myJS = function ()
{
myFunction = function()
{
document.write("Hello world");
};
};
为什么不呢?在此先感谢您的帮助。
最佳答案
在另一个函数内声明的函数不会成为该函数的属性。 如果你想让 myJS 成为一个以 myFunction 作为方法的对象,你可以这样做
myJS = {
myFunction: function()
{
document.write("Hello world");
}
};
关于Javascript html 从外部文件调用外部对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14151203/