使用document.location.hash时如何重置链接?(How to reset the link when using document.location.hash?)

编程入门 行业动态 更新时间:2024-10-19 23:32:29
使用document.location.hash时如何重置链接?(How to reset the link when using document.location.hash?)

我在使用document.location.hash时能够滚动到感兴趣的特定DIV。 但是,当我刷新页面时,我无法将此滚动到相同的DIV。

JS

document.location.hash = 'sendEmailWrap';

链接:

myDomain.com/publication.php?article=658#sendEmailWrap

刷新页面时,链接末尾会显示“#sendEmailWrap”,因此滚动到同一个DIV不起作用。 我该如何获取链接

myDomain.com/publication.php?article=658

I able to scroll to a particular DIV of interest when using document.location.hash. However, when I refresh the page I am unable to get this scrolling to the same DIV.

JS

document.location.hash = 'sendEmailWrap';

Link:

myDomain.com/publication.php?article=658#sendEmailWrap

When I refresh the page the '#sendEmailWrap' persists at the end of the link, as a result the scroll to the same DIV does not work. How do I go about getting the link back to

myDomain.com/publication.php?article=658

最满意答案

如果由于您的内容在页面加载时尚未呈现而无法正常工作,则可以在内容准备好后“重置”哈希。 你可以尝试:

window.onload = function() { var hash = location.hash; location.hash = ''; location.hash = hash; };

或者,在页面加载时简单地删除它:

window.onload = function() { location.hash = ''; };

If it's not working due to your content not being rendered yet when the page loads, you could "reset" the hash after the content is ready. You could try:

window.onload = function() { var hash = location.hash; location.hash = ''; location.hash = hash; };

or, to simply get rid of it when the page is loaded:

window.onload = function() { location.hash = ''; };

更多推荐

本文发布于:2023-08-06 16:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   location   document   hash   link

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!