2011-04-08 140 views
0

我想使用jQuery构建基于锚的导航系统。我真的不想使用plugin like this作为suggested here。任何人都可以帮助我建立像这样的系统的基本逻辑?jQuery锚定导航

基本上我希望能够有一个网页成为像这样通过链接多个AJAX内容:

  • /myurl.html#state1
  • /myurl.html#state2

目前我只是结合的Click处理程序链接打开对话框等我然后可以将URL更改为/myurl.html#state1。问题是,我这样做,或者我只是让<a>标签自己指向该网址,然后使用jQuery来检测URL何时发生变化,这意味着它也可以在页面加载?

任何建议/想法将是巨大的 - 感谢

+4

我知道你可能不想使用插件,但我发现jquery-bbq是最好的这种功能http://benalman.com/projects/jquery- bbq-plugin/ – Calum 2011-04-08 11:37:56

+0

Sans插件,这将是使用http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/ – Calum 2011-04-08 11:40:21

+0

@Calum我发现烧烤之前,th anks的建议,它只是一个页面,尽管如此插件似乎是矫枉过正。我之前也看到了yendesign的帖子,但我真的不喜欢设置计时器来检查更改的想法 - 看起来很浪费 – Chris 2011-04-08 12:25:48

回答

2
<a href="#state1" rel="ajaxpage1.html"></a> 
<a href="#state2" rel="ajaxpage2.html"></a> 

$('a').each(function (i,obj){ 
    $(obj).click(function(){ 
     var url = $(obj).attr('rel'); 
     $..ajax({ 
      url: url, 
      success: function(){...}, 
      error : function(){...}// incase you get a 404, or 500 and so on.... 
     }); 
    }); 
}); 

$(document).ready(function(){ 
    //if you need it on refresh, or if a user got the link from another page 
    // it automatically will trigger the click therefore trigger the ajax 
    if(window.location.hash != '#'){ 
     $('a[href="'+window.location.hash+'"]').click(); 
    } 
}); 

我还是件事你应该使用插件,因为他们有错误,并固定它们可能,所以你必须要测试你的事,浪费很多的时间,但你的电话:)