Given the following selector $('parent > child'), I believe jQuery will first query for all 'child' elements before filtering down to those that are direct descendants of 'parent'. This can be very inefficient.
My first instinct is to use $('parent').find('child'), but the result is obviously not the same as $('parent > child').
Is there a better way to write this selector?
jQuery has a .children()
method that only selects the immediate children.
Also, don't worry about this stuff! Unless you have a gigantic app or it's out of curiosity there's no reason to delve into this. If you are using a selector way too often just "cache" it: $tabs = $('.tabs')
. A good practice is to use $ in front of variables that represent selectors.
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