`
loky
  • 浏览: 176475 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

HTML相对路径(Relative Path)和绝对路径(Absolute Path)

阅读更多
HTML初学者会经常遇到这样一个问题,如何正确引用一个文件。比如,怎样在一个HTML网页中引用另外一个HTML网页作为超链接(hyperlink)?怎样在一个网页中插入一张图片?......

如果你在引用文件时(如加入超链接,或者插入图片等),使用了错误的文件路径,就会导致引用失效(无法浏览链接文件,或无法显示插入的图片等)。

为了避免这些错误,正确地引用文件,我们需要学习一下HTML路径。

HTML有2种路径的写法:相对路径和绝对路径。

HTML相对路径(Relative Path)
同一个目录的文件引用
如果源文件和引用文件在同一个目录里,直接写引用文件名即可。

我们现在建一个源文件info.html,在info.html里要引用index.html文件作为超链接。

假设info.html路径是:c:\Inetpub\wwwroot\sites\blabla\info.html

假设index.html路径是:c:\Inetpub\wwwroot\sites\blabla\index.html

在info.html加入index.html超链接的代码应该这样写:

<a href = "index.html">index.html</a>

如何表示上级目录
../表示源文件所在目录的上一级目录,../../表示源文件所在目录的上上级目录,以此类推。

假设info.html路径是:c:\Inetpub\wwwroot\sites\blabla\info.html

假设index.html路径是:c:\Inetpub\wwwroot\sites\index.html

在info.html加入index.html超链接的代码应该这样写:

<a href = "../index.html">index.html</a>

假设info.html路径是:c:\Inetpub\wwwroot\sites\blabla\info.html

假设index.html路径是:c:\Inetpub\wwwroot\index.html

在info.html加入index.html超链接的代码应该这样写:

<a href = "../../index.html">index.html</a>

假设info.html路径是:c:\Inetpub\wwwroot\sites\blabla\info.html

假设index.html路径是:c:\Inetpub\wwwroot\sites\wowstory\index.html

在info.html加入index.html超链接的代码应该这样写:

<a href = "../wowstory/index.html">index.html</a>

如何表示下级目录
引用下级目录的文件,直接写下级目录文件的路径即可。

假设info.html路径是:c:\Inetpub\wwwroot\sites\blabla\info.html

假设index.html路径是:c:\Inetpub\wwwroot\sites\blabla\html\index.html

在info.html加入index.html超链接的代码应该这样写:

<a href = "html/index.html">index.html</a>

假设info.html路径是:c:\Inetpub\wwwroot\sites\blabla\info.html

假设index.html路径是:c:\Inetpub\wwwroot\sites\blabla\html\tutorials\index.html

在info.html加入index.html超链接的代码应该这样写:

<a href = "html/tutorials/index.html">index.html</a>

HTML绝对路径(Absolute Path)
HTML绝对路径(absolute path)指带域名的文件的完整路径。

假设你注册了域名www.blabla.cn,并申请了虚拟主机,你的虚拟主机提供商会给你一个目录,比如www,这个www就是你网站的根目录。

假设你在www根目录下放了一个文件index.html,这个文件的绝对路径就是: http://www.blabla.cn/index.html。

假设你在www根目录下建了一个目录叫html_tutorials,然后在该目录下放了一个文件index.html,这个文件的绝对路径就是http://www.blabla.cn/html_tutorials/index.html。
分享到:
评论

相关推荐

    HTML相对路径(Relative Path)和绝对路径(Absolute Path)深入理解

    …… 加上struts中相对路径也是大行其道,一不小心很容易搞混淆。 如果在引用文件时(如加入超链接,或者插入图片等),使用了错误的文件路径,就会导致引用失效(无法浏览链接文件,或无法显示插入的图片等)。 为了...

    Linux 相对路径和绝对路径的使用

    绝对路径和相对路径在shell环境中经常遇到,各有用处。有时候相对路径比较方便,有时候绝对路径比较方便。 02. 绝对路径(Absolute Pathname) 绝对路径必定由**/**开头 绝对路径是为档案/文件的所在位置做指向 在...

    path-judge:判断路径是绝对路径还是相对路径

    判断路径是绝对路径还是相对路径以及一些额外的功能。 安装 npm install path-judge --save 开始 pathJudge = require 'path-judge' 接口演示 ### # @param path {string} the path string. # @return {boolean} ...

    Python路径处理库pathlib.docx

    o判断绝对路径 is_absolute() o拼接路径 joinpath(*other) o测试路径符合模式 match(pattern) o计算相对路径 relative_to(*other) o更改路径名 with_name(name) o更改路径后缀 with_suffix(suffix)

    PathKit:在Swift中轻松进行路径操作

    let absolutePath = path. absolute () 规范化路径 这将清除所有多余的..或. 和路径中的双斜杠。 let normalizedPath = path. normalize () 删除路径 path. delete () 移动路径 path. move (newP

    eslint-plugin-absolute-import:强制在导入中使用绝对路径

    规则静态分析禁止使用相对路径([ no-relative-path ])导入模块禁止未解决的进口([ no-unresolved ])安装npm install eslint-plugin-absolute-import -g 或者,如果您将ESLint作为dev依赖项进行管理: # inside ...

    tsc-alias:打字稿编译后将绝对路径替换为相对路径

    tsc-alias 打字稿编译后,将别名路径替换为相对路径。 您可以通过提供baseUrl的相对路径来添加引用tsconfig.json项目之外的其他项目的别名。与比较+编译时间(无运行时相关性)入门首先,使用npm将tsc-alias安装为...

    python中模块查找的原理与方法详解

    前言 本文主要给大家介绍了关于python模块查找的原理与方式,分享出来供大家参考学习,下面话不多说,来一起看看...Python 执行一个 py 文件,无论执行的方式是用绝对路径还是相对路径,interpreter 都会把文件所在的

    fs-plus:节点的fs模块,并添加了一些有用的功能

    absolute(relativePath) 通过针对当前工作目录解析给定的路径,使之为绝对路径。参量String relativePath :表示相对路径的字符串。 如果路径以“〜”为前缀,它将被扩展到当前用户的主目录。返回String :绝对路径...

    getAbsoluteUrl:获取页面中资源完整绝对的url地址 Get absolute url from relative one

    还有一种可以变通的方法就是根据页面得到资源的完整绝对路径。 到底怎么做呢?类似 abc.jpg, ../abc.jpg, /abc.jpg, ../../.abc.jpg 这些如何得到正确的完整地址呢? 解析页面url,得到http头、host、端口、path,再...

    require-resolve:使用节点方式将所需路径解析为绝对路径

    要求解决 使用节点方式将所需路径解析为绝对路径。安装$ npm install --save require-resolve用法如果没有找到则返回null ,否则返回一个对象,包括src和pkg , pkg是可选的。 var requireResolve = require ( '../'...

    更改red5录制视频文件的存放路径

    /** Path that will store recorded videos. */ public String recordPath = "recordedStreams/"; /** Path that contains VOD streams.... /** Set if the path is absolute or relative */ 代码内修改路径

    davidagood.com:个人网站和博客

    David A.良好的网站和博客 用构建。 在此处查看持续集成管道: : 。有用的GraphQL查询query AllFiles { allFile &#40;... { relativePath absolutePath sourceInstanceName }}query SiteData { site { siteMetadata { t

    rollup-plugin-root-import:添加通过根路径导入模块的功能,例如Meteor

    // illustrative - if we were in /views/user, we can reference a model's module// using an absolute path from the root of the project, rather than relative to// the current moduleclass UserView { // .....

    JavaScript将相对地址转换为绝对地址示例代码

    在看LABjs源代码时,发现里面有个将相对地址转为绝对地址的函数,将其拿出纪录如下: 代码如下: function canonical_uri(src, base_path) { var root_page = /^[^?#]*\//.exec&#40;location.href&#41;[0], root_...

    find-babel-config:根据目录查找最接近的babel配置

    // directory can be an absolute or relative path // If it's a relative path, it is relative to the current working directory (process.cwd()) const directory = 'src' ; findBabelConfig ( directory ) . ...

    Clay:很棒HTML&CSS原型制作工具

    粘土 一个了不起的原型制作工具。 使用Clay,您仅需在页脚中添加链接,就可以忘记对数十个HTML文件进行更改。 您还可以使用它来原型化... # Absolute or relative path. clay new myapp / path / to / project / t

    codemods:用于TypeScript和JavaScript代码库的codemod的集合

    将绝对导入转换为相对导入。 - import { Button } @/components/Button` + import { Button } from '../../correct/relative/path/components/Button' 在运行之前,打开absolute-to-relative-imports.js并在文件...

    fltk-1.1.9-gcc3.x-1bibosoft.DevPak

    Fixed wrong relative path when absolute path has a trailing slash in fl_filename_relative (STR #1920) Fixed multiple selction of files and directories in Fl_File_Chooser (STR #1913) Fixed MS...

Global site tag (gtag.js) - Google Analytics