jQuery selector on an item within an object
I am trying to use a jQuery selector on data I get back from an ajax call.
Here is the code I have -
$.ajax({
url: 'moo.html',
success: function ( code )
{
var divs = $(code).filter(function(){ return $(this).is('div') });
console.log( divs ); // gives me back entire object
console.log( $(divs[0]) ); // gives me the first object
// console.log( $(divs[0]).('#bar')); // error
// console.log( $(divs #foo #bar)); // error
// console.log( $(divs).(#foo #bar)); // error
}
});
</script>
moo.html -
<div id='foo'><div id='bar'>123</div><开发者_JAVA百科/div><div id='biz'><div id='dev'>345</div></div>
So how can I grab the html contents (123) of the bar div within the foo div?
var bar = $("#bar", $(code)).text();
or
var bar = $("#bar", $(code)).html();
whichever is appropriate.
You can also approach this problem this way:
var bar = $(code).find("#bar").text();
Not sure if I understand your question but I believe this should do the trick: $("#bar").html();
精彩评论