这是一个相当简单的过程:(1)解析哈希标签,(2)像往常一样通过 Ajax 加载内容。
如果您在用户单击页面时加载更多内容,请确保始终正确修改哈希标记以反映页面上的内容。
这是一个简单的例子。单击一个名称并记下井号标签。相关的 Javascript 如下所示:
// Go straight to content if it's in the hash.
$(document).ready(function(){
load_story_from_hash();
});
// Call this function whenever user clicks on a hash link
function set_hash(hash){
window.location.hash = hash;
load_story_from_hash()
}
// Actually load content based on the hash in the URL
function load_story_from_hash(){
var hash = window.location.hash;
hash = hash.replace(/^#/, '');
if (hash) {
$('#post_container').load(hash+'.html', {}, function(){
$.scrollTo('#post_container', 1000);
});
}
}