Some of the JQUERY applications I did before were very basic programming and did not have high requirements for selectors, such as "$('.class')", "$('#id')", "$( '#id>.class')", "$(this)", "$(this).parent()", "$(this).children()" can all be solved.
The problem arises when I want to select a span in a certain li in ul, because to facilitate programming, li is selected using $('ul>li').eq(i), although the problem It can still be solved using the previous method, but I always feel that the code is a bit bloated and inefficient. Since JQUERY is known as the most efficient code among all current JS frameworks, it is impossible to solve this problem with a lot of code!
Looking back at JQUERY's API, I found that there is a statement that is very suitable for this occasion that I have never noticed. That is: $(selector one).find(selector two). Although I have read the introduction to find before, it has always been ignored because it is not used.
Here’s how to use find()
Explanation provided by API: Search for all elements matching the specified expression. This function is a great way to find out the descendant elements of the element being processed.
Usage: $(selector one).find(selector two);
Take the focus picture on the homepage of Xiaoju Inn, find the span in the DD under the DL with the class ".focusphoto" and fade it out:
Talk about the understanding of find() in the short drama
My application can actually use children() instead, but this does not mean that the usage of find() and children() are the same. If this is the case, the existence of find() will be meaningless. When the query depth is one level, the two have similarities, but the difference is: find() is a subquery selector, which can go deep into the lower level of the child for query selection; and children(): only the child A level selector that can only select elements at the next level.
Of course, the above is just the humble opinion of the people in the drama. If there are any errors or imperfections, please correct me. Thank you!