gpt4 book ai didi

javascript - window.location.reload 后转到 anchor 链接

转载 作者:数据小太阳 更新时间:2023-10-29 04:48:21 24 4
gpt4 key购买 nike

我正在尝试更新哈希,然后重新加载页面。

$('a[name="' + fragment + '"]').remove(); //don't jump before window reloads
window.location.hash = fragment;
window.location.reload(true);

重新加载窗口后不会跳转到 anchor 标记。我该如何解决?

最佳答案

如果您要重新加载页面,这在 jQuery 中实现起来相当简单。加载页面时只需检查 window.location.hash 属性即可。

$(document).ready( function( ) {
if( window.location.hash ) { // just in case there is no hash
$(document.body).animate({
'scrollTop': $( window.location.hash ).offset().top
}, 2000);
}
});

唯一需要注意的是,您的哈希与您滚动到的元素的 ID 相匹配。

演示 here

关于javascript - window.location.reload 后转到 anchor 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com