Found a whole lot of questions on json, nothing specifically asking this (unless I missed it somewhere):
After a JSON request, I have string that comes back with som开发者_StackOverflow中文版e HTML in it:
Returned data:
'<div class="set1">Set1</div><div class="set2">Set2</div><div class="set1">Set1</div><div class="set2">Set2</div>'
On success, I'm setting my data (string) into a jQuery object like: $(data).
How do I actually go about pulling out: Set1 out of that object? Is that even possible?
Quick example:
$.ajax({
type: "POST",
url: "posting to get some json data back",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var newData = $(data);
}
});
Where would I go from here if I want to look at the .text()
of div.set1
?
What you are looking for is jquery's filter
.
http://api.jquery.com/filter/
"Description: Reduce the set of matched elements to those that match the selector or pass the function's test."
$(data).filter('div.set1').text()
Since you passed your HTML to the jQuery constructor, it is now a regular jQuery collection (even though you have not yet added it into the DOM).
Hence, you have access to all of jQuery's regular functions:
$.ajax({
type: "POST",
url: "posting to get some json data back",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var newData = $(data).filter('div.set1').text();
}
});
精彩评论