如何在进入页面时转到特定锚点(不起作用)

发布于 2024-12-22 00:03:16 字数 357 浏览 6 评论 0原文

首先,这就是我目前的状态:thsbrk.de

黑框应该是例如关于部分。我想实现的是,如果您进入我的页面(thsbrk.de),您可以直接转到我的参考部分(锚点“#references”)。然后,如果您点击“关于”链接,您将向上滚动到“关于”部分。我已经尝试让它发挥作用,但没有成功。锚似乎不起作用。

如果有人可以查看我的代码并为我提供解决方案,那就太棒了:)

(滚动尚未实现,我只要求锚点问题)

编辑:这里我有一个示例,它应该如何工作:< a href="http://volll.com/" rel="nofollow">示例

First of all, thats my current state of play: thsbrk.de.

The black boxes should be e.g. a about section. I want to achieve that if you enter my page (thsbrk.de) you directly go to my reference section (anchor '#references'). Then, if you hit the about link you will scroll up to that about section. I already tried to make it working but it doesn't. The anchor seems to be not working.

It would be awesome if someone could look at my code and offer me a solution :)

(The scroll isn't implemented yet, I only ask for the anchor problem)

EDIT: Here I've got a example how it should work: Example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

做个ˇ局外人 2024-12-29 00:03:16

在头部给出一个像这样的脚本标签。也让它成为第一个脚本。

<script>
location.href="http://thsbrk.de/#references"
</script>

从你的代码来看,你也做了同样的事情。但只要尝试重新排序可能有效的脚本标签。

Give a script tag like this in the head.Let it be the first script also.

<script>
location.href="http://thsbrk.de/#references"
</script>

From your code, you have did the same. But just try reordering the script tags it might work.

合约呢 2024-12-29 00:03:16

普通 JS:

window.onload=function() {
  var anchorHash = 'references';
  document.getElementsByName(anchorHash)[0].scrollIntoView();
}

这是 2009 年的 jQuery 示例 - 可能有更新的方法

如何使用 jQuery 将表格的一行滚动到视图 (element.scrollintoView) 中?

在您的情况下,这可能有效

$(document).ready(function() {
  var anchorHash = 'references';
  var pos = $('#'+anchorHash).position();
  window.scrollTo(0,pos.top);
});

Plain JS:

window.onload=function() {
  var anchorHash = 'references';
  document.getElementsByName(anchorHash)[0].scrollIntoView();
}

Here is a jQuery example from 2009 - there may be newer ways

How do I scroll a row of a table into view (element.scrollintoView) using jQuery?

In your case this might work

$(document).ready(function() {
  var anchorHash = 'references';
  var pos = $('#'+anchorHash).position();
  window.scrollTo(0,pos.top);
});
纸伞微斜 2024-12-29 00:03:16

尝试这个并告诉我结果:

$(document).ready(function() {
   window.location.href = '#references';
});

然后像这样修改你的锚标记:

<a name="references">Here</a>

Try this and tell me the result:

$(document).ready(function() {
   window.location.href = '#references';
});

and then modify your anchor tag like this:

<a name="references">Here</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文