【问题标题】:How to implement Ajax back button simulation without href如何在没有href的情况下实现Ajax后退按钮模拟
【发布时间】:2012-01-18 23:49:03
【问题描述】:

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

【问题讨论】:

    标签: jquery hash history


    【解决方案1】:

    您可以使用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');
    }
    

    【讨论】:

    • 触发器正是我想要的,谢谢 Jasper!
    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2013-10-26
    • 2015-02-23
    相关资源
    最近更新 更多