将页面重定向到锚点

13
需要将http://sitename.com/cat/index.php重定向到http://sitename.com/cat/index.php#anchor
我尝试了两种方法:使用PHP重定向和meta刷新。 PHP重定向
<?php
    $URL = "http://sitename.com/cat/index.php#anchor";
    header("Location: $URL");
?>
<html>
....
(元刷新)
<html>
    <head>
    ... 
    <meta http-equiv="refresh" content="0;url=http://sitename.ru/cat/index.php#anchor">
    ...
    </head>
...
</html>

作为两种方式的结果,我的页面不停地刷新。
如何正确解决这个问题?

2
尝试在这里查看解决方案:https://dev59.com/_3RB5IYBdhLWcg3wz6Wd#506004 - mgraph
谢谢。它按照我想要的方式工作。 - v2p
1个回答

9

您不需要重定向页面,只需设置window.location.hash即可。

window.location.hash="anchor";

这是我的问题的正确解决方法。谢谢你。 - v2p
3
不使用 JavaScript 是否有可能解决这个问题? - Hjulle

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