$('.wrapper a').find('a'); //return empty object
But i am looking for a way get all anchors by selector. Problem is find method look at only descendants so what is alternative of it ?
Please test it on jsfiddle.net
jQuery find
gets the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.
children
gets the children of each element in the set of matched elements, optionally filtered by a selector.
I think you are trying to find the elements at the same level then you should use children
. Alternatively you can also use filter
to filter the matched results based on selector.
filter
reduces the set of matched elements to those that match the selector or pass the function's test.
Try this
var div = $('.wrapper div').filter('.parent');
the alternatives of .find()
function which are as below:
$('#parent_id > #child_id')
or $(".parent > .first-level-child")
$('#parent_id #child_id')
or $('#parent_id #grandchild_id')
or $(".parent .great-grand-child")
or $( "form input" )
for detailed info about jquery selectors check JQuery Selectors
$('.wrapper a').find('a');
find links inside links that are descendants of .wapprer. I think you might have meant $('.wrapper').find('a');
. In your fiddle that would be
$('.wrapper').find('.parent');`
insetead of:
$('.wrapper div').find('.parent');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.