For example, when we use $('div span')
, how does jQuery do such a search for us?
Does it search for the span
element first or the div
?
It seems that the jQuery official site has no explanation on these things; it just show us what's functions are available through the API.
I want to know how every type jQuery selector is implemented, so I can select the best selector to use.
Do you know where I can get such info?
jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.
ID Selector: The jQuery #id selector selects the element on the basis of the ID attribute of the HTML tag.
Broadly, what you need to know:
:first-child
are faster than jQuery selectors like :first
.You can also read the official Sizzle documentation, but it doesn't really discuss optimization techniques.
It searches for the span
elements first. It parses the selector as:
For the first part it can use the getElementsByTagName
method to find all span
elements, then it has to loop through the ancestors of each to look for div
elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With