简体   繁体   English

将CSS / Javascript链接到网页的正确方法是什么?

[英]What is the correct way to link the CSS/Javascript to webpage?

I want to link the css and javascript files to my asp.net form. 我想将css和javascript文件链接到我的asp.net表单。 I saw diffrent ways to do that. 我看到了不同的方法。 But dont know the diffrence. 但不知道差异。

  1. With / 随着/

     <link type="text/css" rel="stylesheet" href="/Content/animate.css"> 
  2. With ~/ 随着~/

     <link type="text/css" rel="stylesheet" href="~/Content/animate.css"> 
  3. With nothing 什么都没有

     <link type="text/css" rel="stylesheet" href="Content/animate.css"> 
  4. With ../ 随着../

     <link type="text/css" rel="stylesheet" href="../Content/animate.css"> 

What is the correct way and what are the difference? 什么是正确的方法,有什么区别? Please explain also when to use what? 请解释一下何时使用什么?

正确的方法是“2”,因为你从项目的根开始寻址

<link type="text/css" rel="stylesheet" href="~/Content/animate.css">
  1. With / 随着/

     <link type="text/css" rel="stylesheet" href="/Content/animate.css"> <link type =“text / css”rel =“stylesheet”href =“/ Content / animate.css”> 

    This goes all the way to the root directory. 这一直到根目录。 If the file with that line is example.com/folder/anotherFolder/index.html , then that line of code will access example.com/Content/animate.css . 如果带有该行的文件是example.com/folder/anotherFolder/index.html ,那么该行代码将访问example.com/Content/animate.css It just goes to the very start. 它刚刚开始。

  2. With ~/ 随着~/

     <link type="text/css" rel="stylesheet" href="~/Content/animate.css"> 

    This goes almost to the root like the first example, but stops one folder short. 这几乎像第一个例子一样,但是一个文件夹缩短了。 If the file with that line is example.com/folder/anotherFolder/index.html , then that line of code will access example.com/folder/Content/animate.css . 如果带有该行的文件是example.com/folder/anotherFolder/index.html ,那么该行代码将访问example.com/folder/Content/animate.css

  3. With nothing 什么都没有

     <link type="text/css" rel="stylesheet" href="Content/animate.css"> 

    I think for the most part, you will want this one. 我认为在大多数情况下,你会想要这个。 It accesses files relative to the current one. 它访问相对于当前文件的文件。 If the file with that line is example.com/folder/anotherFolder/index.html , then that line of code will access example.com/folder/anotherFolder/Content/animate.css . 如果带有该行的文件是example.com/folder/anotherFolder/index.html ,那么该行代码将访问example.com/folder/anotherFolder/Content/animate.css

  4. With ../ 随着../

     <link type="text/css" rel="stylesheet" href="../Content/animate.css"> 

    This one backs out just one folder/level. 这个只退出一个文件夹/级别。 If the file with that line is example.com/folder/anotherFolder/index.html , then that line of code will access example.com/folder/Content/animate.css . 如果带有该行的文件是example.com/folder/anotherFolder/index.html ,那么该行代码将访问example.com/folder/Content/animate.css

The difference is that if you site is http://example.com and you have an application http://exmaple.com/app1 . 不同之处在于,如果您的网站是http://example.com并且您有一个应用程序http://exmaple.com/app1

  1. This means root of the site. 这意味着网站的根目录。 So it will become: http://example.com/Content/animate.css 所以它将成为: http://example.com/Content/animate.csshttp://example.com/Content/animate.css

     <link type="text/css" rel="stylesheet" href="/Content/animate.css"> 
  2. The '~' means root of the application. '〜'表示应用程序的根目录。 so it will become: http://example.com/app1/Content/animate.css 所以它将成为: http://example.com/app1/Content/animate.csshttp://example.com/app1/Content/animate.css

     <link type="text/css" rel="stylesheet" href="~/Content/animate.css"> 
  3. This is realtive path so depending on where the file is, it will simply look for a Content folder in that same folder and then animate.css file. 这是一个实际路径,因此根据文件的位置,它只会在同一个文件夹中查找Content文件夹,然后查找animate.css文件。 If you move the file where this is written to another folder, it will still look for a Content folder at that new location and then animate.css file. 如果将文件移动到另一个文件夹,它仍将在该新位置查找Content文件夹,然后查找animate.css文件。

     <link type="text/css" rel="stylesheet" href="Content/animate.css"> 
  4. This is saying go up one directory (from the file wherever this line of code is written) and then find Content folder and then animate.css file. 这就是说上一个目录(从文件所在的任何地方编写代码),然后找到Content文件夹,然后找到animate.css文件。

     <link type="text/css" rel="stylesheet" href="../Content/animate.css"> 

Each one has its place and use. 每个人都有自己的位置和用途。 Now that you know what they mean, you can choose which to use. 现在您知道它们的含义,您可以选择使用哪个。

As a final note, 作为最后的说明,

  1. Write all the different ones in an html file. 在html文件中写下所有不同的。
  2. Open Chrome debugger (or any browser debugger) and select the Network tab. 打开Chrome调试器(或任何浏览器调试器),然后选择“网络”选项卡。
  3. Access the html page form the browser 从浏览器访问html页面
  4. See how the browser interprets each path. 了解浏览器如何解释每个路径。 You will see the browser making requests to each one using the complete URL. 您将看到浏览器使用完整的URL向每个浏览器发出请求。

You can use that technique anytime for any path to see what it gets resolved to. 您可以随时使用该技术获取任何路径以查看其解析的内容。

<link type="text/css" rel="stylesheet" href="../Content/animate.css">

当css文件位于Content之前的文件夹内时使用此选项

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

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