jQuery Filtering Elements (Traversing)

 

The previously mentioned selection elements ( selectors ) are directly from the root element to find the descendant sub-elements under it, and here we are going to discuss how to further "filter", jQuery Traversing related methods can help you do these filtering, access elements Actions.

Traversing is to go further from a certain element to find out the elements related to it.

Filtering

jQuery provides some functions to help us conveniently "filter out" the target element we want:

Get the index element (index starts from 0)

.eq( index )

For example, get the third element of the match

$('p').eq(2);
Compared to .get(index), what you get is a DOM object; .eq(index) is a jQuery object.

Find all elements that meet the expression conditions (multiple selectors can be separated by commas)

.filter(selector)

For example, to get all paragraph elements of the category highlight:

$('p').filter('.highlight');

Delete all elements that meet the expression conditions

.not(selector)

For example, from the selected paragraph elements, delete the elements whose category is green and whose id is blueone:

$('p').not('.green, #blueone');

Post a Comment

0 Comments