javascript脚本
The ‘src’ attribute in a tag is the path to an external file or resource that you want to link to your HTML document.
标记中的'src'属性是您要链接到HTML文档的外部文件或资源的路径。
For example, if you had your own custom JavaScript file named ‘script.js’ and wanted to add its functionality to your HTML page, you would add it like this:
例如,如果您有一个名为'script.js'的自定义JavaScript文件,并想将其功能添加到HTML页面,则可以这样添加:
<!DOCTYPE html>
<html lang="en"><head><title>Script Src Attribute Example</title></head><body><script src="./script.js"></script></body>
</html>
This would point to a file named ‘script.js’ that is in the same directory as the .html file. You can also link to other directories by using ’..’ in the file path.
这将指向名为“ script.js”的文件,该文件与.html文件位于同一目录中。 您也可以在文件路径中使用“ ..”链接到其他目录。
<script src="../public/js/script.js"></script>
This jumps up one directory level then into a ‘public’ directory then to a ‘js’ directory and then to the ‘script.js’ file.
这将跳到一个目录级别,然后跳到“ public”目录,再跳到“ js”目录,再跳到“ script.js”文件。
You can also use the ‘src’ attribute to link to external .js files hosted by a third party. This is used if you don’t want to download a local copy of the file. Just note that if the link changes or network access is down, then the external file you are linking to won’t work.
您还可以使用'src'属性链接到第三方托管的外部.js文件。 如果您不想下载文件的本地副本,则使用它。 请注意,如果链接更改或网络访问断开,则链接到的外部文件将无法工作。
This example links to a jQuery file.
本示例链接到jQuery文件。
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
更多信息: (More Information:)
MDN Article on the HTML
有关HTML的MDN文章
翻译自: https://www.freecodecamp.org/news/link-javascript-to-html-with-the-src/
javascript脚本