A Brief Introduction to the subelement filter selector in jQuery-js tutorial

Source: Internet
Author: User
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


The filter selector can obtain the first child element in the specified parent element. However, the selector returns only one element, not a set.


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.