Html 如何链接相同或不同文件夹中的html页面?

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

How to link html pages in same or different folders?

htmlanchor

提问by AgileJon

How can I link to html pages if they are in same or different folders without writing full path?

如果它们在相同或不同的文件夹中,我如何链接到 html 页面而不写完整路径?

回答by AgileJon

Within the same folder, just use the file name:

在同一个文件夹中,只需使用文件名:

<a href="thefile.html">my link</a>

Within the parent folder's directory:

在父文件夹的目录中:

<a href="../thefile.html">my link</a>

Within a sub-directory:

在子目录中:

<a href="subdir/thefile.html">my link</a>

回答by joeylange

In addition, if you want to refer to the root directory, you can use:

另外,如果要引用根目录,可以使用:

/

Which will refer to the root. So, let's say we're in a file that's nested within a few levels of folders and you want to go back to the main index.html:

这将指的是根。因此,假设我们在一个嵌套在几级文件夹中的文件中,并且您想返回到主 index.html:

<a href="/index.html">My Index Page</a>

Robert is spot-on with further relative path explanations.

罗伯特对进一步的相对路径解释很到位。

回答by swagers

Also, this will go up a directory and then back down to another subfolder.

此外,这将上升到一个目录,然后返回到另一个子文件夹。

<a href = "../subfolder/page.html">link</a>

To go up multiple directories you can do this.

要上多个目录,您可以执行此操作。

<a href = "../../page.html">link</a>

To go the root, I use this

去根,我用这个

<a href = "~/page.html">link</a>

回答by Robert Harvey

You can go up a folder in the hierarchy by using

您可以使用

../

So to get to folder /webroot/site/pages/folder2/mypage.htm from /webroot/site/pages/folder1/myotherpage.htm your link would look like this:

因此,要从 /webroot/site/pages/folder1/myotherpage.htm 访问文件夹 /webroot/site/pages/folder2/mypage.htm,您的链接将如下所示:

<a href="../folder2/mypage.htm">Link to My Page</a>

回答by Chris Ballance

use the relative path

使用相对路径

main page might be: /index.html

主页可能是: /index.html

secondary page: /otherFolder/otherpage.html

二级页面: /otherFolder/otherpage.html

link would be like so:

链接是这样的:

<a href="/otherFolder/otherpage.html">otherpage</a>

回答by than10

If you'd like to link to the root directory you can use

如果你想链接到根目录,你可以使用

/, or /index.html

/, 或者 /index.html

If you'd like to link to a file in the same directory, simply put the file name

如果您想链接到同一目录中的文件,只需输入文件名

<a href="/employees.html">Employees Click Here</a>

To move back a folder, you can use

要移回文件夹,您可以使用

../

To link to the index page in the employees directory from the root directory, you'd do this

要从根目录链接到员工目录中的索引页面,您可以这样做

<a href="../employees/index.html">Employees Directory Index Page</a>

回答by jrharshath

I would caution you: if you are using absolute paths, then your application cannot be installed in a "subdirectory" of the server!

我要提醒您:如果您使用的是绝对路径,那么您的应用程序不能安装在服务器的“子目录”中!

eg, http://yourserver.com/yourappmay work, but http://myserver.com/apps/yourappwill not!

例如,http: //yourserver.com/yourapp可能会起作用,但http://myserver.com/apps/yourapp不会!

回答by Syntaxsizer

Use

../

For example if your file, lets say image is in folder1in folder2you locate it this way

例如,如果您的文件,假设图像folder1folder2您以这种方式定位

../folder1/folder2/image

回答by adem

Short answer:

简短的回答:

.is for current directory

. 用于当前目录

..is for upper directory as in cd ..command on shell.

..用于上层目录,如cd ..shell上的命令。

Simple yet tricky, I write this answer primarily for myself not to forget next time.

简单而棘手,我写这个答案主要是为了自己下次不要忘记。

ademSite/
├── index.html
└── style.css

The link to CSSin index.html:

链接到CSSindex.html

<link rel="stylesheet" href="style.css">or <link rel="stylesheet" href="./style.css">

<link rel="stylesheet" href="style.css">或者 <link rel="stylesheet" href="./style.css">

ademSite/
├── index.html
└── stylefiles
    └── style.css

This case it should be:

这种情况应该是:

<link rel="stylesheet" href="stylefiles/style.css">or <link rel="stylesheet" href="./stylefiles/style.css">

<link rel="stylesheet" href="stylefiles/style.css">或者 <link rel="stylesheet" href="./stylefiles/style.css">

├── html
│?? └── index.html
└── stylefiles
    └── style.css

In this case path mustbe: <link rel="stylesheet" href="../stylefiles/style.css">

在这种情况下,路径必须是: <link rel="stylesheet" href="../stylefiles/style.css">

回答by Harry

For ASP.NET, this worked for me on development and deployment:

对于 ASP.NET,这对我的开发和部署有用:

<a runat="server" href="~/Subfolder/TargetPage">TargetPage</a>

Using runat="server"and the href="~/"are the keys for going to the root.

使用runat="server"href="~/"是进入根目录的键。