以 html 中的正斜杠开头,表示“href”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10659459/
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-29 00:30:54  来源:igfitidea点击:

Starting with a forward slash in html for "href"

html

提问by anc1revv

I started learning html recently, and one thing that really confused me is why do some links have a forward-slash("/") before the path and some links don't?

我最近开始学习 html,让我感到困惑的一件事是为什么有些链接在路径前有一个正斜杠(“/”)而有些链接没有?

ie.

IE。

<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">

vs.

对比

<dt><a href="reset/index.html">Reset CSS</a></dt>

Is one a relative path and one an absolute path? and how do href's work exactly? does it just stick on the path name after the base url?

一个是相对路径,一个是绝对路径?以及 href 究竟是如何工作的?它是否只是贴在基本 url 之后的路径名上?

回答by Mark Byers

Is one a relative path and one an absolute path?

一个是相对路径,一个是绝对路径?

Yes.

是的。

If your browser is currently pointing at http://foo/bar/baz.htmlthen:

如果您的浏览器当前指向,http://foo/bar/baz.html则:

  • <a href="reset/index.html">would link to http://foo/bar/reset/index.html.
  • <a href="/reset/index.html">would link to http://foo/reset/index.html.
  • <a href="reset/index.html">将链接到http://foo/bar/reset/index.html.
  • <a href="/reset/index.html">将链接到http://foo/reset/index.html.


If there is a base elementin the head of your HTML document then the relative path will be relative to the base. For example the link here will take you to http://example.com/foobar/reset/index.htmlregardless of where the page is located.

如果HTML 文档的头部有一个base 元素,那么相对路径将是相对于 base 的。例如,http://example.com/foobar/reset/index.html无论页面位于何处,此处的链接都会带您前往。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
 <HEAD>
   <TITLE>Base element example</TITLE>
   <BASE href="http://example.com/foobar/">
 </HEAD>

 <BODY>
   <P><a href="reset/index.html">Reset CSS</a>
 </BODY>
</HTML>