如何在没有href的情况下实现Ajax后退按钮模拟

时间:2011-12-13 02:17:44

标签: jquery hash history

因此我的整个站点都运行在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来跟踪网址中的哈希值,但是当我找到的所有内容都是锚标记示例时,我不确定如何执行此操作。有人能指出我正确的教程方向,谢谢!

1 个答案:

答案 0 :(得分:2)

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

$('.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');
}
相关问题