In the example of jQuery’s find()
(the doc is at http://api.jquery.com/find/)
I wonder why we need it, because, is
$('li.item-ii').find('li')
the same as
$('li.item-ii li')
essentially, all the <li> elements inside the <li> element that has the class item-ii
Similarly
$('#id1').find(".class1 #id2")
is the same as
$('#id1 .class1 #id2")
?
Update: if they are the same, why do we need find() ? is it because mainly when we need to further select some elements when given another element in a variable, so we can do elementFoo.find("...")?
Yes, these are equivalent. The result is the same, it’s called the descendant selector. Their utility differs a little bit (e.g. using
.end(),.andSelf(), etc), but for just selecting elements, you’re finding the same set.For kicks, you can also do this:
For your edit: Why do we need it? Sometimes you’re not coming from a selector, quick example:
There are many times you need to find something relatively, not from a known selector (could be a DOM element,
this, aniframedocument, etc). There are many other traversal functions for this as well.