2011-03-14 96 views

回答

3

AJAX調用不會返回DOM元素。它發送一個異步HTTP請求給服務器端腳本返回其可以是任何類型的HTML的結果,XML,JSON,....然後這個結果被傳遞到success回調處理:

$.get('/script', function(result) { 
    // TODO: do something with the result returned by the server script 
}); 

因此,例如,如果你的服務器端腳本返回一些HTML內容,你要提取您可以做這樣的特定元素:

$.get('/script', function(result) { 
    // get an element with id="foo" from the returned result 
    var foo = $(result).find('#foo'); 
}); 
2

達林是正確的。但是,您可能正在尋找由load(Ajax方法而不是事件處理程序)提供的功能。

諸如$('#result').load('ajax/test.html #container');之類的東西會解析ID爲container的元素的響應HTML並將其插入到result的HTML中。 (load)()也接受加載完成時將調用的回調)。

$('#result').load('ajax/test.html #container,.classname', 
        function(){ 
         $('#result').html(); //this should give you the DOM that matches either .classname or #container 
        });