2012-05-19 48 views
1

我正在使用wordpress,并且我想在点击任何本地链接后更改网址。我用ajax加载页面,这就是为什么我想要这样做。
我可以使url更改在“http://site.com/”和加载的内容/ example-page /之间添加一个哈希值,结果为:“http://site.com/#/example-page”并加载示例页面,但我想添加“!”标记以获得“http://site.com/#!/example-page”just like this theme
我使用jquery-hashchange插件顺便说一句。
请让我知道您的想法。

如何更改散列/#/ for /#!/

此代码在解决后编辑,所以这是正确的代码。

jQuery(document).ready(function($) { 
    var $mainContent = $("#container"), 
    siteUrl = "http://" + top.location.host.toString(), 
    url = ''; 
    $(document).delegate("a[href^='"+siteUrl+"']:not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/])", "click", function() { 
    location.hash = '!' + this.pathname; 
    return false; 
    }); 
    $("#searchform").submit(function(e) { 
    location.hash = 's/' + $("#s").val(); 
    e.preventDefault(); 
    }); 
    $(window).bind('hashchange', function(){ 
    url = window.location.hash.substring(3); 
    if (!url) { 
     return; 
    } 
    url = url + " #content"; 
    $mainContent.animate({opacity: "0.1"}).html('Please wait...').load(url, function() { 
     $mainContent.animate({opacity: "1"}); 
    }); 
    }); 
    $(window).trigger('hashchange'); 
}); 
+1

欢迎的网站 - 我们的标记解决问题的方法是,以纪念一个答案使用公认的勾号图标,而不是编辑问题说它已经解决了。 – BoltClock

回答

3

变化

location.hash = this.pathname; 

location.hash = "#!" + this.pathname; 

(严格来说,你应该有散有前)和改变

url = window.location.hash.substring(1); 

url = window.location.hash.substring(2); 

,改变

location.hash = '?s=' + $("#s").val(); 

location.hash = '#!?s=' + $("#s").val(); 
+0

非常感谢尼尔。其实我找到了同样的答案嘿嘿。 –

+0

location.hash ='?s ='+ $(“#s”)。val();不适用于我:/ –

+0

@CarlosSalas我已经更新了我的答案,原始版本中有错误。 – Neil