[英]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. 我的ajax(jquery)响应(html)给了我一大堆html源代码,因为它获取了enrite页面。
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>
? 1)是否可以读取
<div id='content'></div>
之间的任何<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>
? 2)如果#1不可能,我怎样才能从
<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 非常感谢,Racky
那这个呢?
$("#content", results).html()
Yes, it is possible, but you need to make a jQuery object out of it first. 是的,这是可能的,但你需要首先从中创建一个jQuery对象。
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. 看起来如果你想要的元素是
body
的直接子元素,你需要使用.filter()
代替,因为body
似乎被排除在jQuery对象之外。 Looks like just its contents are included. 看起来只包含其内容。
alert ( $(results).filter("#content").html() );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.