Home > Web Front-end > JS Tutorial > jQuery:nth-child selector usage example_jquery

jQuery:nth-child selector usage example_jquery

WBOY
Release: 2016-05-16 16:22:38
Original
1523 people have browsed it

The example in this article describes the usage of :nth-child selector in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This selector matches the Nth child or odd-even element under its parent element.
The
:eq(index) selector only matches one element, while the :nth-child selector will match child elements for every parent element.
:nth-child starts from 1, and :eq() starts from 0.
Grammar structure:

Copy code The code is as follows:
$(":nth-child")

This selector is generally used in conjunction with other selectors, such as class selectors, element selectors, etc. For example:

Copy code The code is as follows:
$("li:nth-child(2)").css("color ","blue")

The above code can set the font color in the second li element under the parent element to blue. It is also possible to calculate odd and even child elements of a parent element. For example:

Copy code The code is as follows:
$("li:nth-child(even)").css("color ","blue")

The above code can set the font color in the even-numbered li element under the parent element to blue.

Note: It is necessary to explain the concept in combination with the above code. The parent element mentioned here is not li, but the parent element of li.
Many people often mistakenly think that it matches the last element among the child elements of the li element.

Example code:

Copy code The code is as follows:






:nth-child selector-Script Home






  • ASP Tutorial

  • ASP.NET Tutorial

  • ;PHP Tutorial



  • html tutorial

  • DIV CSS tutorial

  • jQUERY Tutorial

  • jAVAScript Tutorial






I hope this article will be helpful to everyone’s jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template