jQuery - 在跳转链接后重新加载页面

发布于 2024-08-27 08:25:45 字数 230 浏览 6 评论 0原文

我正在使用隐藏的 div 创建幻灯片效果。单击缩略图后,相应的 div 将出现在窗口中,而其他 div 将被隐藏。但是,我还需要重新加载页面。我尝试使用这样的东西:

$("a").click(function() {
    location.reload();
});

但是,这将重新加载页面而不跟随链接(类似于 href="#div02")。如何让它跟随链接并重新加载页面?

I'm creating a slideshow effect using hidden div's. Once a thumbnail is clicked, the corresponding div appears in a window and the other divs are hidden. However, I also need the page to reload. I attempted to use something like this:

$("a").click(function() {
    location.reload();
});

However, this will reload the page without following the link (something like a href="#div02"). How do I get it to both follow the link and reload the page?

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

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

发布评论

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

评论(3

养猫人 2024-09-03 08:25:45

如果我理解正确的话,你想要的是修改浏览器的 URL 以反映 DOM 的当前状态。这意味着,如果用户单击图片,URL 将通过附加“#div02”进行更新,最终用户将能够与朋友共享此链接,这将直接将他们带到该图片/状态。

如果这就是您想要的,那么您正在寻找位置哈希。设置:

window.location.hash = "#div02";

获取:

window.location.hash

If I understand you correctly, what you want is to modify the URL of the browser to reflect the current state of the DOM. Meaning, if a user clicked on a picture, the URL would be updated by appending a "#div02" and finally the user would be able to share this link with friends which will take them directly to that picture/state.

If that's what you want, your looking for Location Hash. To set:

window.location.hash = "#div02";

To get:

window.location.hash
岁月苍老的讽刺 2024-09-03 08:25:45

无需重新加载页面,只需删除嵌入上一个视频的元素即可。

Rather than reload the page just remove the element that embeds the previous video.

止于盛夏 2024-09-03 08:25:45

您可以向当前网址添加额外参数(带有随机值)。由于您的网址已更改,浏览器将重新加载页面,然后跳转到锚点。

var randomString = Date.now();
window.location = "?something=" + randomString + "#div02";

You can add an extra parameter (with random value) to to your current url. The browser will reload the page and then jumb to an anchor since your url has changed.

var randomString = Date.now();
window.location = "?something=" + randomString + "#div02";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文