简体   繁体   English

如何在不同的文件中执行 javascript?

[英]How to execute javascript in different file?

I have multiple javascript functions in my html page.我的 html 页面中有多个 javascript 函数。 One of them, by far the biggest, is executed on a button click.其中一个,迄今为止最大的,是在单击按钮时执行的。 If I moved that function into a separate.js file, what would be the syntax to have the onclick run that file?如果我将 function 移动到一个单独的 .js 文件中,那么让 onclick 运行该文件的语法是什么?

If the name of the function is still the same, then you don't have to do anything except include the script in your html somewhere before any code uses it, like so:如果 function 的名称仍然相同,那么您无需执行任何操作,只需在任何代码使用它之前将脚本包含在 html 的某处,如下所示:

<script src="/yourscript.js" type="text/javascript"></script>

Your javascript functions can be in any number of files.您的 javascript 函数可以在任意数量的文件中。 You just need to add the reference to the.js file like this inside the tag:您只需要在标签内添加对 .js 文件的引用:

<script type="text/javascript" src="path/to/scripts/filename.js"></script>

You don't have to change anything.您无需更改任何内容。

Just include your javascript file in your HEAD section, and the rest may remain the same.只需将 javascript 文件包含在HEAD部分中,rest 可能保持不变。 :-) :-)

Your onclick doesn't run the file.您的onclick没有运行该文件。 It runs the function contained within that file.它运行该文件中包含的 function。 You wouldn't need to change the onclick syntax to make that happen.您无需更改onclick语法即可实现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM