How to get child elements of $self, jquery is OK
$('#cont > fieldset').each(
function(index开发者_JS百科){
var $self = $(this);
// Here how to get child elements? How to write this selector?
//$('$self > div') ?? this seems does not work.
});
$self.find("div"); // return all descendant divs
or:
$self.children("div"); // return immediate child divs
depending on whether you want immediate children or any descendants.
You can even do this to get immediate child divs, but children
is prettier :
$self.find(">div");
Look at the .children
method in jQuery. This will get direct children of the element, e.g.:
$self.children('div') // returns divs that are direct children
You can also use the similar .find
method if you need to go deeper than one level.
$self.find('div') // returns divs that are direct children, or children of children
Also, you can select using $self
as the context, like:
$('div', $self) //returns all divs within $self
using children
$(this).children('div')
or using find
$(this).find('div');
look on this post
You can use the children() method, to get all immediate children of self.
var children = $self.children();
精彩评论