Home > Web Front-end > JS Tutorial > jQuery selector: detailed explanation of the use of last-child

jQuery selector: detailed explanation of the use of last-child

黄舟
Release: 2017-06-23 14:44:06
Original
1571 people have browsed it

Overview

Match the last child element

':last' matches only one element, while this selector will match one child element for each parent element

Example

Description:

Find the last li in each ul

HTML code:

<ul>
<li>John</li> 
<li>Karl</li> 
<li>Brandon</li> 
</ul> 
<ul> 
<li>Glen</li> 
<li>Tane</li> 
<li>Ralph</li> 
</ul>
Copy after login

jQuery code:

$("ul li:last-child")
Copy after login

Result:

[ <li>Brandon</li>, <li>Ralph</li> ]
Copy after login

This selector can match the last child element of the parent element.

Note: last only selects one element, and this selector will match the last child element of each parent element.
Grammar structure:

$(":last-child")
Copy after login

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

$("li:last-child").css("color","blue")
Copy after login

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

Note: It is necessary to explain the concept in conjunction 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:











  • ASP教程
  • ASP.NET教程
  • PHP教程
  • html教程
  • DIV+CSS教程
  • jQUERY教程
  • jAVAScript教程
Copy after login

The above is the detailed content of jQuery selector: detailed explanation of the use of last-child. For more information, please follow other related articles on the PHP Chinese website!

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