2014-01-14 31 views
0

我想添加一些哈希參考鏈接,當我點擊菜單按鈕改成了實際的部分也許從阿賈克斯/ jQuery的位置

http://www.mycompany.com/abouthttp://www.mycompany.com/work也許/東西

我有這樣的解決方案但我不知道如何將此添加到我的實際解決方案中。

$(document).ready(function(){ 
    $('#allcontent').load('data/home.html'); 

    $('.hovers').click(function() { 
     var page = $(this).attr('href'); 
    $('#allcontent').load('data/' + page + '.html'); 
    return false; 
}); 
}); 

這裏是我的小提琴:DEMO

這個

也許? window.location.hash =

謝謝你這麼多

+0

可能的重複[如何Github更改頁面和URL如此順利沒有AJAX?](http://stackoverflow.com/questions/5216314/how-is-github-changing-pages-and-the-url-如此順利 - 沒有ajax) – Quentin

+0

無法理解問題。你會詳細解釋這個問題嗎? – Jai

+0

非常感謝你!你是我的英雄! – andre34

回答

2

我認爲你正在尋找的是通過AJAX加載網頁內容,並仍然能夠網址更新爲/something

http://www.mycompany.com/abouthttp://www.mycompany.com/work也許/東西

要做到這一點,你只需要通過做這樣的事情來加載你想要的頁面的內容和改變位置的URL到所需的頁面名稱,

<script type="text/javascript"> 
function processAjaxData(response, urlPath){ 
    document.getElementById("content").innerHTML = response.html; 
    document.title = response.pageTitle; 
    window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath); 
} 
</script> 

REF:Modify the URL without reloading the page

一個website這實際上已經實現了這個。 他們正在做的是,通過ajax點擊菜單項加載頁面內容。 然後通過js設置瀏覽器網址。他們甚至將菜單鏈接保存爲/pages 而不是#values,因爲這會提升seo。

+0

非常感謝!這幫助了我的問題:) – andre34