如果您打算劫持它们,则不需要将哈希添加到您的 url。在我的 div#container 中使用正常链接,使用此实现似乎一切正常:
$("#container a").live('click', function(){
var hrefRequested = $(this).attr( "href" );
// Push this URL "state" onto the history hash.
$.bbq.pushState({ url: hrefRequested });
// Prevent the default click behavior.
return false;
});
// Bind a callback that executes when document.location.hash changes.
$(window).bind( "hashchange", function(e) {
var url = $.bbq.getState( "url" );
// Used to make sure that when you back up to the page you came in on
// you don't get an empty page.
if ( url == undefined ) { url = window.location.href; }
$('#content-container').load(url + ' #container', function(response, status, xhr) {
if (status == "success") {
console.log('hooray!');
// document.title = ;
// document.location.href = hrefRequested;
// return false;
}
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
// You probably want to actually do something useful here..
});
// Since the event is only triggered when the hash changes, we need
// to trigger the event now, to handle the hash the page may have
// loaded with.
$(window).trigger("hashchange");