What's the difference between $ ('. Classname') and $ .find ('. Classname')

Sometimes $('.classname')

they $.find('.classname')

give different results. I wonder what is the difference between them.

+3


source to share


3 answers


$('.classname')

      

will search the DOM for elements with a class classname

and

$.find('.classname')

      



will give you the error of doing nothing, why

.find (selector)

the string selectorA containing the selector expression to match elements against. version added: 1.6.

.find (jQuery object) A jQuery objectJQuery to map elements. Added version: 1.6

.find (element) elementAn to match elements with.

+6


source


I can not tell you,

but you can see the source code for $. find and $. fn.find

$.find

This is a technique used by jQuery itself and is not recommended for use as a query selector.



$.fn.find

Refers to $.find

in the main structure: (where this

is a jQuery object)

var ret = this.pushStack("", "find", selector),
    length, n, r;

for (i = 0, l = this.length; i < l; i++) {
    length = ret.length;
    jQuery.find(selector, this[i], ret);

    ...

      

+2


source


$('.classname')

will return a list of all the elements with that class name in the document so that they can be iterated over with .each()

.

$.find('.classname')

searches the dom tree and returns class events. But you will need to provide a parent, for example:

$(document).find('.classname')

+1


source







All Articles