当前位置:首页 > jquery

jquery nth

2026-02-04 03:19:08jquery

jQuery :nth Selector

jQuery provides several selectors to target elements based on their position in a group. The :nth selectors include :nth-child(), :nth-of-type(), :nth-last-child(), and :nth-last-of-type(). These selectors are useful for styling or manipulating specific elements in a list or group.

:nth-child()

The :nth-child() selector matches elements based on their position within a parent element. It accepts an argument that can be a number, a keyword (like odd or even), or a formula.

Syntax:

$("selector:nth-child(index)")

Example:

// Selects the 3rd child of all <li> elements
$("li:nth-child(3)").css("color", "red");

// Selects every odd <tr> in a table
$("tr:nth-child(odd)").addClass("highlight");

:nth-of-type()

The :nth-of-type() selector matches elements of a specific type based on their position among siblings of the same type.

Syntax:

$("selector:nth-of-type(index)")

Example:

jquery nth

// Selects the 2nd <p> element in a container
$("p:nth-of-type(2)").hide();

// Selects every even <div> in a section
$("div:nth-of-type(even)").css("background", "yellow");

:nth-last-child()

The :nth-last-child() selector works like :nth-child(), but counts from the last child instead of the first.

Syntax:

$("selector:nth-last-child(index)")

Example:

// Selects the 2nd last <li> in a list
$("li:nth-last-child(2)").addClass("highlight");

// Selects every 3rd child from the end
$("div:nth-last-child(3n)").fadeOut();

:nth-last-of-type()

The :nth-last-of-type() selector matches elements of a specific type, counting from the last sibling of that type.

jquery nth

Syntax:

$("selector:nth-last-of-type(index)")

Example:

// Selects the last but one <span> in a container
$("span:nth-last-of-type(2)").css("font-weight", "bold");

// Selects every 4th <div> from the end
$("div:nth-last-of-type(4n)").slideUp();

Using Formulas

The :nth selectors support formulas like an + b, where a and b are integers. This allows for more complex selections.

Example:

// Selects every 3rd element starting from the 2nd
$("li:nth-child(3n+2)").toggleClass("active");

// Selects elements from the 4th onwards
$("p:nth-child(n+4)").hide();

Practical Use Cases

  • Highlighting specific rows or items in a list.
  • Applying alternating styles (zebra-striping) to tables or lists.
  • Targeting elements dynamically based on their position.

These selectors are powerful tools for DOM manipulation, especially when dealing with repetitive structures like lists, tables, or grids.

标签: jquerynth
分享给朋友:

相关文章

jquery库

jquery库

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

jquery 面试题

jquery 面试题

jQuery 面试题常见考点 DOM 操作 选择器使用:$("#id"), $(".class"), $("tag") 元素增删改查:append(), remove(), html(),…

jquery选择器

jquery选择器

jQuery 选择器基础 jQuery 选择器用于选取 DOM 元素,基于 CSS 选择器语法扩展。通过 $() 或 jQuery() 函数调用,返回匹配元素的集合。 常见选择器类型 元素…

jquery 图表

jquery 图表

jQuery 图表库推荐 jQuery 本身不直接提供图表功能,但可以通过插件或集成其他库实现图表绘制。以下是几种常用的 jQuery 图表解决方案: Chart.js 集成 Chart.js 是…

jquery的

jquery的

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它的核心理念是“写得更少,做得更多”(Write Le…

jquery 元素

jquery 元素

jQuery 元素选择与操作 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。以下是 jQuery 中常用的元素选择与操作方法…