This article mainly introduces the subelement filter selector in jQuery, and lists some methods to obtain the parent element when operating the DOM, you can refer to the filter rules of the child element filter to obtain the corresponding elements through the relationship between the parent element and the child element.
$ ('Li: first-child'hangzhou.css ('background', '# ccc'); // The first li element for each parent element $ ('li: last-child'hangzhou.css ('background ', '# ccc'); // The Last li element of each parent element $ ('li: only-child'background .css ('background ',' # ccc '); // each parent element has only one li element $ ('li: nth-child(odd1_'hangzhou.css ('background', '# ccc'); // an odd number of li elements for each parent element $ ('li: nth-child(even='background .css ('background ',' # ccc '); // an even number of li elements for each parent element $ ('li: nth-child(2%'background .css ('background', '# ccc '); // The third li element of each parent Element
We know that
:first
The filter selector can obtain the first child element in the specified parent element. However, the selector returns only one element, not a set.
:first-child
The child element filtering selector can obtain the first child element returned by each parent element. It is a set and is often used for data selection and processing of multiple sets.
For example, if you want to obtain the first li in each ul in the page and change its color. You can use
: first-child
The text color of the first line in the hundred-year-old "Vegetables and Fruits"
- Celery
- Eggplant
- Radish
- Chinese Cabbage
- Tomato
- Orange
- Bananas
- Grape
- Apple
- Watermelon