Jquery gets the code for a particular child of an element

  • 2020-03-30 04:27:24
  • OfStack

Do some JQUERY before application programming, is all some very basic requirements for selector is not high, as is similar to "$(' class ')", "$(' # id)", "$(' # id> .class) ", "$(this)", "$(this). The parent ()", "$(this). The children ()" can solve all these simple statement.

The problem is that I'm going to choose one of the li's in a ul, because for programming purposes, li is using $('ul> Li '). Eq (I), although the problem can still be solved by the previous method, but always feel a little bloated code, and very inefficient. Since JQUERY is known as the most efficient code in any JS framework today, it's impossible to solve this problem with a whole bunch of code!

Going back to the JQUERY API, there was one statement that I hadn't noticed that was perfect for this situation. That is: $(selector 1). Find (selector 2). Although I have read find before, it has been ignored because it is not available.

Here's how to use find()
An explanation provided by the API: searches for all elements that match the specified expression. This function is a good way to find the descendants of the elements you are working with.

Usage: $(selector 1). Find (selector 2);

Take the focus picture of the home page of small drama inn, find the span in DD under the DL of the class ". Focusphoto "and fade it out as an example:


$('.focusphoto>dl>dd).eq(i).find( " span " ).fadeOut('slow');

Talk about the play's understanding of find()

My application could actually use children() instead, but that doesn't mean that find() and children() are used in the same way, which would make the existence of find() meaningless. When the query depth is one level, the two have something in common, but the difference is that find() is a subquery selector, which can go to the lower level of the subquery to select; Children (): is just a child selector, which can only select elements at the next level.

Of course, the above is just a bit of the play people's opinions, if there is a mistake or imperfect place, please correct, thank you!


Related articles: