如何实现无href的Ajax后退按钮模拟

发布于 2024-12-21 08:57:49 字数 498 浏览 2 评论 0原文

因此,我的整个网站运行在一个 index.php 页面上,并且根据用户想要执行的操作,通过许多 AJAX 调用动态加载内容。我想使用后退按钮,但目前不能。我读过一些很棒的教程,并搜索了一些与此相关的很棒的 Stackoverflow 问题。我的问题是这样的。我看到的所有教程,包括我倾向于的 Jquery BBQ,似乎在所有教程中都使用了 href,但我根本不使用锚标记。

我的一个典型功能......就是说加载评论。

$('.comments').live('click', function() {
    var id = this.id;
    // pass the unique id of the class to a php function, return result
});

我知道我将使用 id 来跟踪网址中的哈希值,但当我找到的只是锚标记示例时,我不确定如何执行此操作。有人可以为我指出一个好的教程的正确方向吗,谢谢!

So my entire site runs off of an index.php page and the content loads dynamically with many AJAX calls depending on what the user wants to do. I want to use the back button but currently I cannot. I have read some great tutorials and scoured over some great Stackoverflow questions regarding this. My problem is this. All the tutorials I'm seeing, including the one I'm leaning to towards, Jquery BBQ, seem to use href in all the tutorials but I don't use anchor tags at all.

A typical function of mine....to say load a comment.

$('.comments').live('click', function() {
    var id = this.id;
    // pass the unique id of the class to a php function, return result
});

I know I will use the id to keep track of the hash in the url, but I'm not sure how to do this when all I'm finding is anchor tag examples. Can someone point me in the right direction of a good tutorial, thanks!

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

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

发布评论

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

评论(1

不乱于心 2024-12-28 08:57:49

您可以使用 window.location 对象的 hash 属性:

$('.comments').live('click', function() {
    var id = this.id;
    window.location.hash = id;
    // pass the unique id of the class to a php function, return result
});

然后,如果用户刷新页面或深层链接到网站:

if (typeof(window.locaion.hash) != 'undefined') {
    //now you know there is a hash and you can trigger a click on the corresponding element
    $('#' + window.locaion.hash).trigger('click');
}

You can use the hash property of the window.location object:

$('.comments').live('click', function() {
    var id = this.id;
    window.location.hash = id;
    // pass the unique id of the class to a php function, return result
});

And then if a user refreshed the page or deep-links into the site:

if (typeof(window.locaion.hash) != 'undefined') {
    //now you know there is a hash and you can trigger a click on the corresponding element
    $('#' + window.locaion.hash).trigger('click');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文