Reading a portion of the ajax html response
My ajax (jquery) response (html) gives me a whole junk of html source code since it fetches the enrite page.
The response is somewhat like below:
<html>
<head>
...
...
</head>
<body>
.
.....
.
.......
<div id="content">
content i want to extract
</div>
.............
..........
.............
</body>
</html>
I need help with the following:
1) Is it possible to read just whatever is between the <div id='content'></div>
? if yes, how?
2) if #1 is not possible, how could I extract just the content from the <div id='content'></div>
?
The code I tried:
/*Ajax*/
$jq(function(){
alert ("Doc ready");
$jq("#content a.next-page").bind("click", function(e){
alert ("Hey!");
/*Make the call*/
$jq.ajax({
url: "/page/2",
type: "get",
cache: false,
data: "",
error: function(){alert ("No data found for your search.");},
success: function(results){
//$searchPanel.find("tbody").empty().append(results);
//$searchPanelHolder.css({"display":"block"});
alert (results.find("div[id='content']").html());
e.preventDefault();
开发者_开发技巧 }
});
e.preventDefault();
});
});
Any help on this is much appreciated
Many thanks, Racky
what about this?
$("#content", results).html()
Yes, it is possible, but you need to make a jQuery object out of it first.
Change this line:
alert (results.find("div[id='content']").html());
to this:
alert ( $(results).find("div[id='content']").html() );
or, better yet, to this:
alert ( $(results).find("#content").html() );
EDIT:
Looks like if the element you want is a direct child of body
, you'll need to use .filter()
instead since the body
seems to be excluded from the jQuery object. Looks like just its contents are included.
alert ( $(results).filter("#content").html() );
精彩评论