当前网址的子目录的href

5
假设当前的网址是:www.my.com/somewhere
我有一个href链接,想让它跳转到:www.my.com/somewhere/subpage,而不需要在href中写入当前网址(www.my.com/somewhere)。
这是否可能只使用纯HTML(不使用JavaScript或服务器端操作)?
5个回答

3

2020 - 此问题的当前状态:

如果当前URL是 www.my.com/somewhere 缩写 ./subpage 或仅 subpage 将导航到 www.my.com/subpage

如果当前URL以 / 结尾,则会按照问题所询问的方式导航。

www.my.com/somewhere 与 www.my.com/somewhere/ 不同。

那么如何从 www.my.com/somewhere 导航到 subpage? 使用:somewhere/subpage

原始答案:最初的回答


0

我认为./subpage应该能够完成工作。


可以这样做,但为了简洁起见,./是不必要的,最好省略。 - Paul Feakins

0
不,相对链接是相对于您当前的目录和domain.com/somewhere是一个文件,而domain.com/somewhere/是一个目录。
由于/somewhere是一个文件,当前目录变为'/',到'subpage'的相对链接变为链接到'domain.com/subpage'。
如果您使用目录而不是文件来组织页面,则'/somewhere/'将成为您的当前目录。到'subpage/'的链接现在将相对于'/somewhere/',并且会打开'www.mydomain.com/somewhere/subpage/'。但是这可能需要“服务器端操作”,因此:不,我认为不可能链接到当前文档的子页面。

-1
您可以在href中简单地编写相对路径,如下所示:<a href='subpage'></a>

那个链接指向 www.my.com/subpage。我需要进入当前网址的子文件夹。 - Deckard
你在html的<head>标签中指定了<base href="http://www.my.com/" />吗? - Thomas Ghesquiere
那个链接指向www.my.com/subpage。我需要进入当前网址的子文件夹。不,如果您已经在/somewhere中,则“subpage”将转到“/somewhere/subpage”。这个答案是正确的,但它已经被投票否决了。请修改。 - Paul Feakins

-2

除非我误解了你的意思,否则你只需编写 href="/subpage" 即可。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接