最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery "find" method alternative - Stack Overflow

programmeradmin4浏览0评论
$('.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

$('.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

Share Improve this question edited Aug 31, 2011 at 14:55 JaredPar 755k151 gold badges1.3k silver badges1.5k bronze badges asked Aug 31, 2011 at 14:51 AnyOneAnyOne 9312 gold badges12 silver badges41 bronze badges 7
  • 2 What exactly are you trying to select? – Shef Commented Aug 31, 2011 at 14:55
  • It is quite spesific to tell it. I have simply $('.wrapper .spesific') object and i want to query .spesific elements which is inside that object. – AnyOne Commented Aug 31, 2011 at 14:57
  • 2 You already have them with the selector $('.wrapper .spesific'). From your jsFiddle example, what do you want the var div to hold? – Shef Commented Aug 31, 2011 at 14:58
  • @Shef - This is only example. If a method gets an object which is $('.wrapper .spesific') so i want to get .spesific for filtering in that object. filter method is right method. – AnyOne Commented Aug 31, 2011 at 15:00
  • Okay, this has been resolved then by my answer and @ShankarSangoli's one. – Shef Commented Aug 31, 2011 at 15:02
 |  Show 2 more ments

4 Answers 4

Reset to default 9

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');

Looking for this?

var div = $('.wrapper div').filter('.parent');

A forked demo of yours

the alternatives of .find() function which are as below:

  1. Child Selector (“parent > child”) it selects only first-level descendants or direct child elements. for example $('#parent_id > #child_id') or $(".parent > .first-level-child")
  2. Descendant Selector (“ancestor descendant”) it selects a child, grandchild, great-grandchild, and so on, of that element. in it you can use $('#parent_id #child_id') or $('#parent_id #grandchild_id') or $(".parent .great-grand-child") or $( "form input" )
  3. .filter() only search in those elements that match the precondition.
  4. .parent() get the parent of each element in the current set of matched elements, optionally filtered by a selector.
  5. .children() it works exactly the same way as find, but it will only find first-level-children, not more distant descendants.
  6. .closest() get the closest (first) element that matches the selector, starting at the current element.

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');
发布评论

评论列表(0)

  1. 暂无评论