两个js文件之间函数互相调用

xiaoxiao2021-02-28  25

这里是解决方案,把下面的代码保存为同一目录下a.htm, a.js, b.js三个文件,然后打开a.htm看效果。

a.htm的内容---------------------------------------------------<html><head><script src=a.js type=text/javascript></script></head>

<body></body>

</html>

a.js 的内容-------------------------------------------------function load_b(){         // 先把b.js调入

var head = document.getElementsByTagName('head');var testScript = document.createElement('script');testScript.src = 'b.js';testScript.type = 'text/javascript';head[0].appendChild(testScript);

         // 现在就可以调用b.js中的test()函数了

test();}window.onload = load_b;

 

b.js 的内容------------------------------------------------function test(){   alert(bad);}

转载请注明原文地址: https://www.6miu.com/read-2650369.html

最新回复(0)