开发者

jQuery ajax call - Returning particular HTML elements

When making a call to jQuery 开发者_StackOverflow社区get, how does one specify HTML elements to be returned?


An AJAX call doesn't return DOM elements. It sends an asynchronous HTTP request to a server side script which returns a result which could be of any type HTML, XML, JSON, .... This result is then passed to the success callback for processing:

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

So for example if your server side script returned some HTML content and you wanted to extract a particular element you could do this:

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


Darin is right. However, you might be looking for the functionality provided by load (the Ajax method not the event handler).

Something like $('#result').load('ajax/test.html #container'); will parse the response HTML for the element with the id container and insert it into the HTML of result.

You could maintain an invisible div and extract specific DOM elements out of it after load (load)() also accepts a callback that will be called when the load is finished).

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜