使用链接和脚本标签来引用 JavaScript 源代码有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12178429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:19:03  来源:igfitidea点击:

What's the difference between using link and script tag to reference JavaScript source?

javascript

提问by Aske B.

I've tried using both the following source-reference-lines. They both compile. But what is the difference?

我已经尝试使用以下两个源参考行. 他们都编译. 但有什么区别呢?

1st method:

第一种方法:

<script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script>

2nd method:

方法二:

<link href="~/Scripts/jquery-1.4.1.js" type="text/javascript" />


Note: There's also the similar Difference between scriptand link as="script"tagswhich asks about <link href="js/script.js" as="script">, which is different.

注意:询问 about 的标签之间scriptlink as="script"也有类似的差异<link href="js/script.js" as="script">,这是不同的。

采纳答案by Ahsan Khurshid

linktag is used to relate stylesheets or any other linked documents instead of including javascript files.

link标签用于关联样式表或任何其他链接文档,而不是包含 javascript 文件。

The HTML Link Element<link>specifies relationships between the current document and other documents. Possible uses for this element include defining a relational framework for navigation and linking the document to a style sheet.

HTML链接元素<link>指定当前文档和其他文档之间的关系。此元素的可能用途包括定义用于导航和将文档链接到样式表的关系框架。

relAttribute:

rel属性:

This attribute names a relationship of the linked document to the current document. The attribute must be a space-separated list of the link types values. The most common use of this attribute is to specify a link to an external style sheet: the rel attribute is set to stylesheet, and the href attribute is set to the URL of an external style sheet to format the page. WebTV also supports the use of the value next for rel to preload the next page in a document series.

此属性命名链接文档与当前文档的关系。该属性必须是以空格分隔的链接类型值列表。此属性最常见的用途是指定指向外部样式表的链接:rel 属性设置为样式表,href 属性设置为外部样式表的 URL 以设置页面格式。WebTV 还支持使用 rel 的 next 值来预加载文档系列中的下一页。

Possible Values:

可能的值:

  • alternate- An alternate version of the document (i.e. print page, translated or mirror)

  • stylesheet- An external style sheet for the document

  • start- The first document in a selection

  • next- The next document in a selection

  • prev- The previous document in a selection

  • contents- A table of contents for the document

  • index- An index for the document

  • glossary- A glossary (explanation) of words used in the document

  • copyright- A document containing copyright information

  • chapter- A chapter of the document

  • section- A section of the document

  • subsection- A subsection of the document

  • appendixAn appendix for the document

  • helpA help document

  • bookmarkA related document

  • shortcut iconA related (favorite icon) image of the document

  • 替代- 文档的替代版本(即打印页面、翻译或镜像)

  • 样式表- 文档的外部样式表

  • start- 选择中的第一个文档

  • next- 选择中的下一个文档

  • prev- 选择中的上一个文档

  • 内容- 文档的目录

  • index- 文档的索引

  • 词汇表- 文档中使用的词的词汇表(解释)

  • 版权- 包含版权信息的文档

  • 章节- 文档的一章

  • section- 文档的一部分

  • subsection- 文档的一个小节

  • appendix文档的附录

  • 帮助帮助文档

  • bookmark一个相关文档

  • 快捷方式图标文档的相关(收藏夹图标)图像

WhileThe HTML Script Element<script>is used to embed or reference an executable script within an HTML or XHTML document.

HTML脚本元素<script>用于在 HTML 或 XHTML 文档中嵌入或引用可执行脚本。

回答by Daniel A. White

The second (using link) shouldn't work or run and is non-standard.

第二个(使用link)不应该工作或运行并且是非标准的。

http://jsfiddle.net/qMKUv/

http://jsfiddle.net/qMKUv/

回答by cyber8200

  • <link>for CSS
  • <script>for JS
  • <link>用于 CSS
  • <script>对于 JS