2012-05-19 30 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