Javascript Backbone.js路由器向后/向前不工作

Javascript Backbone.js路由器向后/向前不工作,javascript,jquery,ajax,backbone.js,router,Javascript,Jquery,Ajax,Backbone.js,Router,我的Javascript function ajaxPushUrl(thisobj) {} function updatePage(url) { if ($('body').find('.ajax-content-column').length == 0) { // has NO certain class, use normal page request. window.location.href=url; return true;

我的Javascript

function ajaxPushUrl(thisobj) {}


function updatePage(url) {
    if ($('body').find('.ajax-content-column').length == 0) {
        // has NO certain class, use normal page request.
        window.location.href=url;

        return true;
    }

    if (url.toLowerCase().indexOf('page') >= 0) {
        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'html',
            success: function(data) {
                $('.ajax-content-column').html(data);

                return false;
            }
        });
    }
}


var AppRouter = Backbone.Router.extend({
    routes: {
        "page:id.php": "displayPage",
        "*actions": "defaultRoute" // matches http://example.com/#anything-here
    },

    initialize: function(options) {
        console.log('initialized');
    },

    displayPage: function(id) {
        console.log('load page id: '+id);
        return false;
    },
});

// Initiate the router
var app_router = new AppRouter;

app_router.on('route:displayPage', function(id) {
    console.log('page id: '+id);
    return false;
});
app_router.on('route:defaultRoute', function(actions) {
    console.log(actions);
    return false;
});

Backbone.history.start({pushState: true, root: "/ajax-backbone-js/"});


$(function() {
    /**
     * @link http://stackoverflow.com/questions/9328513/backbone-js-and-pushstate
     */
    $('a.ajax-link').click(function(e) {
        var href = $(this).attr('href');
        var protocol = this.protocol + '//';

        if (href.slice(protocol.length) !== protocol) {
            e.preventDefault();
            app_router.navigate(href, true);
            alert('h');
            updatePage(href);
            $('title').text($(this).text());
        }
    });
});
我的html

<ul>
    <li><a href="index.php">Home</a></li>
    <li><a href="page1.php" class="ajax-link" onclick="return ajaxPushUrl($(this));">Page 1</a></li>
    <li><a href="page2.php" class="ajax-link" onclick="return ajaxPushUrl($(this));">Page 2</a></li>
    <li><a href="page3.php" class="ajax-link" onclick="return ajaxPushUrl($(this));">Page 3</a></li>
    <li><a href="page4.php" class="ajax-link" onclick="return ajaxPushUrl($(this));">Page 4</a></li>
</ul>
从主页>点击第1、2、3、4页,效果良好。
但单击“后退”和“前进”不起作用

我只想使用主干路由器,没有视图,没有模型,没有集合。
请帮忙。

我发现这很有效

function ajaxPushUrl(thisobj) {}

function updatePage(url) {
    if ($('body').find('.ajax-content-column').length == 0) {
        // has NO certain class, use normal page request.
        window.location.href=url;

        return true;
    }

    if (url.toLowerCase().indexOf('page') >= 0) {
        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'html',
            success: function(data) {
                $('.ajax-content-column').html(data);

                return false;
            }
        });
    }
}

var AppRouter = Backbone.Router.extend({
    routes: {
        "page:id.php": "displayPage",
        "*actions": "defaultRoute" // matches http://example.com/#anything-here
    },

    initialize: function(options) {
        console.log('initialized');

        $(document).on('click', 'a.ajax-link', function(e) {
            console.log('ajax link clicked');
            var href = $(this).attr('href');
            var protocol = this.protocol + '//';

            if (href.slice(protocol.length) !== protocol) {
                if ($('body').find('.ajax-content-column').length !== 0) {
                    e.preventDefault();
                    app_router.navigate(href, true);
                    $('title').text($(this).text());

                    return false;
                }
            }
        });
    },

    displayPage: function(id) {
        console.log('load page id: '+id);
    },
});

// Initiate the router
var app_router = new AppRouter;

app_router.on('route:displayPage', function(id) {
    var href = Backbone.history.fragment;

    console.log('page id: '+id);
    console.log(href);

    if ($('body').find('.ajax-content-column').length == 0) {
        console.log('no ajax-content-column class');
    } else {
        updatePage(href);

        // lookup link of this ajax page and set title.
        $('title').text($('.menu a[href$="'+href+'"]:first').text());
    }
    return false;
});
app_router.on('route:defaultRoute', function(actions) {
    console.log(actions);
    return false;
});

Backbone.history.start({pushState: true, root: "/ajax-backbone-js/"});

您熟悉主干历史记录文档吗@kinakuta我没有看到它如何与back/forward一起工作并在页面上获取内容的示例。啊,没关系-我在您的代码示例中没有向下滚动足够远,所以我没有看到您在哪里调用history.start()。