jQuery Traversal - Filtering
- Previous page jQuery Colleagues
- Next page Introduction to jQuery AJAX
Abbreviate search element scope
The three most basic filtering methods are: first(), last(), and eq(), which allow you to select a specific element based on its position in a set of elements.
Other filtering methods, such as filter() and not(), allow you to select elements that match or do not match a specified standard.
jQuery first() method
The first() method returns the first element of the selected element.
The following example selects the first <p> element within the first <div> element:
Example
$ (document).ready(function () { $("div p").first(); });
jQuery last() method
The last() method returns the last element of the selected element.
The following example selects the last <p> element within the last <div> element:
Example
$ (document).ready(function () { $("div p").last(); });
jQuery eq() method
The eq() method returns the element with the specified index number in the selected elements.
The index number starts from 0, so the index number of the first element is 0, not 1. The following example selects the second <p> element (index 1):
Example
$ (document).ready(function () { $("p").eq(1); });
jQuery filter() method
The filter() method allows you to specify a standard. Elements that do not match this standard will be removed from the set, and matching elements will be returned.
The following example returns all <p> elements with the class name "intro":
Example
$ (document).ready(function () { $("p").filter(".intro"); });
jQuery not() method
The not() method returns all elements that do not match the standard.
Tip:The not() method is the opposite of filter().
The following example returns all <p> elements without the class name "intro":
Example
$ (document).ready(function () { $("p").not(".intro"); });
jQuery Traversal Reference Manual
To learn all jQuery traversal methods, please visit our jQuery Traversal Reference Manual.
- Previous page jQuery Colleagues
- Next page Introduction to jQuery AJAX