2014-02-22 21 views
0

我第一次嘗試ajax(並在一定程度上js),所以我不知道我做錯了什麼。如何使用ajax定位特定的ID?

目標是在側邊欄中的一系列縮略圖將在選中時更改位於中央的div的內容。該鏈接目前看起來像這樣:

<img src="images/thumbs/elon-scissors-logo.jpg" onclick="reloadMiddleWith('about','about','test')"/> 

點擊後,它運行下面的函數,並更改背景並調用一個PHP源代碼。

function reloadMiddleWith(theme, page, content) { 
    var new_url = "http://www.chrisjohndesigns.com/" + page + ".php/#" + content; 
    $('#live-area').addClass(theme); 
    $.ajax({ 
     url: new_url, 
     dataType: 'html' 
}) 
    .done(function(data) { 
     // Assuming the request returns HTML, replace content 
     $('#area-loader').html(data); 
}); 

} 

我測試了一下它工作得很好時,這只是背景和新的PHP頁面,而不是讓50頁中調用(而且目前甚至沒有試圖對缺乏適當的陣列或數據庫理解)我以爲我只是做1或2個新的頁面,重複相同的容器風格,只是調用我想通過id標籤顯示的那個。目前,當我嘗試它,它只是改變背景,但不會改變PHP的內容。

+0

請求成功嗎?數據的外觀如何? – thescientist

回答

0

jQuery的本身有一個方便的功能jQuery(...).load,使用它的jQuery會處理所有必要的點:

function reloadMiddleWith(theme, page, content) { 
    var new_url = "http://www.chrisjohndesigns.com/" + page + ".php/#" + content; 
    $('#live-area').addClass(theme); 
    jQuery("#area-loader").load(new_url); 
} 

的更多信息,請documentation page出來。

+0

考慮鏈接文檔頁面https://api.jquery.com/load/ – thescientist

+0

@thescientist:感謝您的反饋,我將其添加到答案。 –

相關問題