我该如何控制window.location.hash

时间:2010-09-17 14:16:23

标签: jquery

我的导航包含以下值:

<a href="#home" id="home" class="goto">Home</a>
<a href="#profile" id="profile" class="goto">Profile</a>
<a href="#account" id="account" class="goto">Account</a>
<a href="#feedback" id="feed" class="goto">Feedback</a><br><br><div id="target"></div>

现在我有以下功能:

$(".goto").live("click", function(){
var goto = $(this).attr("id");
 $.post(goto+".php", {}, function(data){
$("#target").html(data);
});

});

它工作正常,但是当我回击浏览器时它没有做任何事情,而我想要回击或转发浏览器内容应该改变上述功能。

请不要将我转移到另一个类似的问题或页面,让我知道确切的解决方案。

1 个答案:

答案 0 :(得分:2)

由于所有浏览器都不支持,我会抓住hashchange plugin by Ben Alman,然后您可以添加:

$(window).hashchange( function(){
  $(location.hash).click();
});

如果在点击后退按钮后哈希说#home,则会触发该锚点的click处理程序,导致当前处理程序执行并加载该内容。此外,如果您不需要POST,(因为您没有传递任何变量),您也可以简化click处理程序,如下所示:

$(".goto").live("click", function(){
  $("#target").load(this.id+".php");
});