本文介绍了重新加载当前页面的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以有一个指向当前位置的普通链接?

Is it possible to have a normal link pointing to the current location?

我目前找到了 2 个解决方案,但其中一个包含 JavaScript,另一个您必须知道页面的绝对路径:

I have currently found 2 solutions, but one of them includes JavaScript and in the other you have to know the absolute path to the page:

<a href="#" onclick="window.location.reload(true);">1</a>
<a href="/foobar/">2</a>
<a href="#">3 (of course not working)</a>

有没有办法做到这一点,而不使用 JavaScript 或不知道绝对路径?

Is there any way of doing this, without using JavaScript or knowing the absolute path?

推荐答案

我一直在使用:

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

尚未找到无法按预期工作的案例和/或浏览器.

Have yet to find a case and/or browser where it does not work as intended.

Period 表示当前路径.您也可以使用 .. 来引用当前路径上方的文件夹,例如,如果您有这样的文件结构:

Period means the current path. You can also use .. to refer to the folder above the current path, for instance, if you have this file structure:

page1.html
folder1
    page2.html

然后你可以在 page2.html 中写:

You can then in page2.html write:

<a href="../page1.html">link to page 1</a>

我不确定行为是否已经改变,或者是否一直如此,但 Chrome(可能还有其他人)会将上述句点视为关于目录,而不是文件.这意味着如果你在 http://example.com/foo/bar.html 你真的在目录 /foo/ 和一个 href<bar.html. 的/code> 值将引用 /foo/ 而不是 bar.html

I'm not sure if the behaviour has changed or if it was always like this, but Chrome (and maybe others) will treat periods as described above as regarding directories, not files. This means that if you are at http://example.com/foo/bar.html you are really in the directory /foo/ and a href value of . in bar.html will refer to /foo/ rather than bar.html

将其视为在终端中导航文件系统;你永远不能cd进入一个文件:)

Think of it as navigating the file system in a terminal; you can never cd into a file :)

编辑 2:

似乎使用 href="." 的行为不再那么可预测了,Firefox 和 Chrome 可能已经改变了它们处理这些的方式.我不会完全依赖我的原始答案,而是针对您的特定用途在不同浏览器中尝试空字符串和句点,并确保您获得所需的行为.

It seems like the behaviour of using href="." is not as predictable anymore, both Firefox and Chrome might have changed how they handle these. I wouldn't rely entirely on my original answer, but rather try both the empty string and the period in different browsers for your specific use and make sure you get the desired behaviour.

这篇关于重新加载当前页面的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:45