The
.children(selector) method returns all children (only children ) of each element in the set of matching elements. Parameters are optional. Adding parameters means filtering through the selector and filtering elements.
The.find(selector) method returns the descendants of each element in the matching element set. The parameter is required and can be a selector, jquery object or element to filter elements.
.find() is similar to the .children() method, except that it only traverses a single level down the DOM tree. Children here, I understand as sons, are only traversed at the son level. Look at the example: .find(selector) method returns the descendants of each element in the set of matching elements. Parameters are required and can be selectors, jquery objects or elements to filter elements. .find() is similar to the .children() method, except that it only traverses a single level down the DOM tree. Children here, I understand as sons, are only traversed at the son level. Take a look at the example:
.children The (selector) method returns all children (only children) of each element in the set of matching elements. Parameters are optional. Adding parameters means filtering through the selector and filtering elements.
The
$( 'ul.level-2').children().css('background-color', 'red');
The result of this line of code is that items A, B, C get red background. Since we didn't apply a selector expression, the jQuery object returned contains all child elements. If a selector is applied, only matching items will be included.
Looking at an example:
This is the expected result, but if you replace the above
, see the code comments above, the result is:
Things to note about the .find() method:
1. Find is to traverse the descendants of each element in the current element collection. As long as they meet the requirements, it doesn't matter whether they are sons or grandsons.
2. Unlike other tree traversal methods, selector expressions are required parameters for .find(). If we need to retrieve all descendant elements, we can pass the wildcard selector '*'.
3. find only traverses the descendants, excluding itself.
4. The selector context is implemented by the .find() method; therefore, $('li.item-ii').find('li') is equivalent to $('li', 'li.item -ii').
The syntax of the selector is: jQuery(selector, [context])
Generally, jquery selectors are used as the first parameter. In fact, this usage of the jquery() function can also pass a second parameter. The purpose of passing this parameter is to limit the previous selector to the context environment. By default, that is, if the second parameter is not passed, the selector searches the DOM from the root of the document ($() will search for DOM elements in the current HTML document); if the second parameter is specified, such as a DOM element Set or jquery object, it will be searched in this context.
Look at an example below
For more information about find(), please visit: http://www.w3school.com.cn/jquery/traversing_find.asp
children() For more information, please visit: http://www.w3school.com.cn/jquery/traversing_children.asp