1

我不知道如何将历史插件功能添加到我的(非常简单的)ajax 代码中。

到目前为止,这是我的代码:

$(function() {

    $.ajaxSetup({ cache: true });
    var hijax = $('ul.hijax a');
    var loader = $('<div id="spinner"></div>');
    hijax.click(function(e){
      hijax.removeClass('ajax-on');
        $(this).addClass('ajax-on');
        var url = $(this).attr('href') + ' #biog-container';
        $("#ajax-container").html(loader).load(url);
        e.preventDefault();
    });
});

这会拉取链接页面的内容,其中包含一个 id 为“biog-containers”的 div(这是一个带有人传记链接的名称列表 - 这是一个巴洛克管弦乐队的网站!)。

ajax 调用效果很好(耶 jQuery!),但我已经用谷歌搜索并搜索了关于添加对历史插件的支持和(我是愚蠢的前端人员)我只是想不通。

谢谢您的帮助。

4

1 回答 1

1

您可以创建一个全局数组,保存通过脚本单击的项目。在每次点击时推送元素,但已经呈现最后一个项目不会是退货项目。

在返回点,检查您的数组是否有多个项目,拉出一项并将其丢弃(该项目代表当前页面),拉出另一项并触发点击它。

此代码未经测试!:

$(function() {
    var links = new Array(),
    hijax = $('ul.hijax a'),
    loader = $('<div id="spinner"></div>'),
    container = $('#ajax-container'),
    content = container.html();

    $.ajaxSetup({ cache: true });

    hijax.click(function(e){
        var $this = $(this),
        url = $this.attr('href') + ' #biog-container';

        e.preventDefault();
        links.push($this);

        hijax.removeClass('ajax-on');
        $this.addClass('ajax-on');
        container.html(loader).load(url);
    });

    $('#back-button').click(function(e){
        var link;
        e.preventDefault();

        if (links.length > 1) {
            // This represents the current link
            links.pop();
            // This represents the past link
            link = links.pop();
            link.click();
        }
        else {
            // This may be empty, or represent the current content
            links.pop();
            // Show the original content
            container.html(content);
        }
    });
});
于 2011-01-10T04:35:41.593 回答