2011-12-02 61 views
2

我有一個頁面,點擊邊欄中的鏈接時加載信息段。使用外部鏈接自動加載頁面+ ajax調用(使用Jquery)

http://www.youarehere.im/other-financial-services.html

所有的工作正常。

我也想鏈接到index.html文件中的這個頁面,但是自動加載相關段落,而不是默認狀態。

因此,在主頁上,「accounts」鏈接加載「other-financial-services.html」,並自動向服務器請求ajax調用「accounts」鏈接。

任何想法?我知道這個jquery代碼片段是完全錯誤的,但我對前端腳本非常陌生,並且在黑暗中拍攝!

HTML:

<div class="box accounting"><h2><a id="service_accounts">Accounting</a></h2></div> 

JQuery的:

$(document).ready(function() { 
    $.ajaxSetup({ 
     cache: false 
    }); 

    var ajax_load = "<img src='img/load.gif' alt='loading...' />"; 
    var load_services = "other-financial-services.html"; 
    var accounts = "accounts.php"; 

    $('#service_accounts').click(function() { 
     $.get(load_services).load(accounts); 
    }); 
}); 
+1

在您的示例頁面URL工具條不FF8爲我工作。 –

+0

僅供參考:您的元內容標籤未關閉,並且您在ajax.js(第46行,'.get(load_services).load(accounts);')中導致菜單無法在FF8和IE9中工作 – syserr0r

+0

Rory,感謝您在meta標籤上的單挑。我知道Ajax.js在我發佈時已經破裂,現在已經修復。 –

回答

1

我認爲你應該這樣做:

<div class="box accounting"><h2><a id="service_accounts">Accounting</a></h2></div> 
<div id='target'></div> 

$('#service_accounts').click(function(){ 
    //load the html page int the div with id=target (of course is just an example) 
    $('#target').load(load_services); 
    $.get(accounts, function(data){ 
     //do something with data returned from the server 
    }); 
}); 
+0

除了一些JS錯誤,他有AJAX子菜單工作,據我所知,他想直接鏈接到__sub-menu__從另一個頁面 – syserr0r

0

如果我理解正確的話,你想這些變量來改變取決於你從哪裏打開頁面?

var load_services = "other-financial-services.html"; 
var accounts = "accounts.php"; 

這意味着您需要找到一種方法將參數傳遞到此頁面,以便您可以在值之間切換。 Unfortuantely,我沒有任何像樣的PHP知識,但我認爲它應該是可能的

?ajaxloadselector=1 

添加到您的網址是什麼?

然後,您可以在鏈接到此頁面的頁面中編輯URLS,然後在此頁面上根據傳遞的'ajaxloadselector'參數製作一個填充變量的開關。

+0

據我所知,他希望能夠鏈接到__sub- menu__頁面直接,這就是問題所在。 – syserr0r

+0

哪一個通過頁面上的Ajax加載,不是嗎?如果我誤解了,道歉:-) – Flater

+0

我可能誤解了你的回答,我的歉意:$ – syserr0r

0

在主頁上,如果你要鏈接到「金融服務」頁面的「會計」子菜單選項,讓像一個鏈接:

http://www.youarehere.im/other-financial-services.html#accounts

和js代碼類似

var url = document.location.toString(); 
if (url.match('#')) { // the URL contains an anchor 
    // click the navigation item corresponding to the anchor 
    var anchor = '#' + url.split('#')[1]; 
    $('#load_' + anchor).click(); 
} 

所以,每當你加載一個頁面URL中的#<something>,元素#load_<something>點擊從http://blog.rebeccamurphey.com/2007/12/04/anchor-based-url-navigation-with-jquery(和修改)無恥地竊取

代碼

+0

我真的很喜歡這個解決方案的想法,不幸的是我無法讓JS工作。按照建議在document.ready()'code' $(document).ready(function(){if(url.match('#define) ')){//該URL包含一個錨點 //單擊與錨點對應的導航項目 var anchor ='#'+ url.split('#')[0]; $('#load_'+錨點).click(); } –

+0

我已經看到您對代碼所做的更改,您需要將上述代碼放在$(document).ready(function(){...)的底部。 });'因爲上面的代碼嘗試'點擊'現在沒有做任何事情的鏈接,因爲點擊事件還沒有設置(它在文件的後面) – syserr0r

+0

在你的代碼中你有'url.split(' #')[0]',它應該是'url.split('#')[1]',因爲您希望文本__after__在我的示例中爲''''accounts',所以它會在'#load_accounts'上'點擊') – syserr0r