gpt4 book ai didi

javascript - 删除 hashchange 事件的 URL 哈希(不是变量)

转载 作者:行者123 更新时间:2023-11-29 10:36:37 27 4
gpt4 key购买 nike

我正在使用 jQuery hashchange 事件,我页面中的每个按钮都使用此代码作为指向另一个页面的链接:

window.location.hash = '#anotherpage';

当我需要返回主页时,location.hash 会在我的 URL 中保留哈希字符 (#),即使 location.hash = nulllocation.hash = ""

这里有人知道在这种情况下如何删除哈希吗?

编辑:

正如 Dsafds 和 Daniel 所说,只有使用 window.location.hash = "#" 才能轻松回到我的主页。问题是主页链接中的字符 (#),用于其他页面,如#contact,是正常的,但我的主页也有哈希值 (site.com/#)。我需要删除它。还有其他想法吗?谢谢!

最佳答案

您可以使用它返回主页:

window.location.hash="/" || window.location.hash="/home"

在你的情况下:

window.location.hash = "#"

编辑

我针对您遇到的问题制定了一个解决方法......关于删除哈希值。

所以您可以做的是在更改“位置”时设置完整 Url。例如这个:

假设您想转到登录页面:

window.location.href = "https://mysite/#/login"

现在你想回到主页!然后你可以这样做:

window.location.href = "https://mysite/"

关于javascript - 删除 hashchange 事件的 URL 哈希(不是变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517441/

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