以“href”的html中的正斜杠开始 [英] Starting with a forward slash in html for "href"
问题描述
我最近开始学习html,有一件事让我非常困惑,那就是为什么一些链接在路径和某些链接之前没有正斜杠(/)?
即
< link href =/ favicon.png =icon>
< link href =/ stylesheets / screen.cssmedia =screen,projection =stylesheettype =text / css>
vs。
< dt>< a href =reset / index.html>重设CSS< / a>< / dt>
是相对路径还是绝对路径?以及href究竟如何工作?它只是坚持路径名后的基础网址?
是一个相对路径和一个绝对路径?
是。
如果您的浏览器是目前指向 http://foo/bar/baz.html
然后:
-
< a href =reset / index.html>
会链接到http://foo/bar/reset/index.html
-
< a href =/ reset / index.html>
会连结至http://foo/reset/index.html
。
如果有基本元素您的HTML文档的头部,那么相对路径将相对于基础。例如,无论网页位于何处,此处的链接都会带您到 http://example.com/foobar/reset/index.html
。
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 // ENhttp://www.w3.org/ TR / HTML4 / strict.dtd>
< HTML>
< HEAD>
< TITLE>基本元素示例< / TITLE>
< BASE href =http://example.com/foobar/>
< / HEAD>
< BODY>
< P>< a href =reset / index.html>重设CSS< / a>
< / BODY>
< / HTML>
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?
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?
Is one a relative path and one an absolute path?
Yes.
If your browser is currently pointing at http://foo/bar/baz.html
then:
<a href="reset/index.html">
would link tohttp://foo/bar/reset/index.html
.<a href="/reset/index.html">
would link tohttp://foo/reset/index.html
.
If there is a base element in 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.html
regardless of where the page is located.
<!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>
这篇关于以“href”的html中的正斜杠开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!