In this example we’re going to talk about the
method of jQuery.
What this method does is kind of self-explanatory, it gets the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. A descendant is a child, grandchild, great-grandchild, and so on.
It has only one argument, which is obviously required in order to make possible element matching and apply the desired methods afterwards.
No matter where the element we want is found, the method will traverse downwards along descendants of DOM elements, all the way down to the last descendant.
1. Document Setup
To begin, create a new HTML document and add the following basic syntax inside:
Note that the jQuery library is linked locally. To download it, please go here.
2. Basic Method Application
In order to apply the method, first get to know the basic syntax:
Let’s add a ul element in HTML and some other elements inside like
, , and .
In the JS section, select the ul element and apply the
method specifying as a filter.
As you can see, we’ve applied some styling to the selected element. The result would be:
3. Cases and Examples
The following examples will all have the same HTML that we will declare here:
Here, we have created a hierarchy representation of various elements which will be used to refer or filter to, in three different cases. Note that the layout has changed, the
tag is now inside the HTML section and the JS section has been moved apart right after .
Also, to make this a little more noticable let’s also style the boxes of these elements:
3.1 Return Multiple Descendants
Whenever you want to find several descendants you can simply separate selectors with commas, and include either tags or classes/id’s like so:
The result is linked to the HTML we defined before, so all
s, st items and elements with a class of would be selected and applied the css styles of red border and text color:
3.2 Return all descendant elements of <html>
In addition to selecting one or more HTML elements using the
method, you may as well want to select all of them. This can be done by specifying the syntax of an asterisk inside the brackets.
The result would show every single box selected (red color of text and red border color):
3.3 Filter the Selection with a given jQuery Collection or Element
As of jQuery 1.6, we can also filter the selection with a given jQuery collection or element. We can do that like so:
As you can see, we’ve declared a variable which will reference all
elements when required. Then, we added the variable name as the argument of the method. The result would show only span elements having the CSS styles applied:
4. Conclusion
To conclude, given a jQuery object that represents a set of DOM elements, the
method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements. The and methods are similar, except that the latter only travels a single level down the DOM tree.5. Download
Download
You can download the full source code of this example here:jQuery Find
You can download the full source code of this example here:jQuery Find
Commentaires
Enregistrer un commentaire