当前位置:首页 > 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:

// 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.

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:

jquery nth

// 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 交互。其核心特点是“Write Less, Do More”,…

jquery选择器

jquery选择器

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

jquery的

jquery的

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

jquery时间

jquery时间

jQuery 时间处理 jQuery 本身不提供专门的时间处理函数,但可以通过 JavaScript 的 Date 对象结合 jQuery 的事件和方法来实现时间操作。以下是常见的时间处理需求及实现方…

jquery 样式

jquery 样式

jQuery 样式操作 jQuery 提供了多种方法来操作 HTML 元素的样式,包括直接修改 CSS 属性、添加或移除类、获取样式值等。 修改 CSS 属性 使用 css() 方法可以直接修改元素…

jquery 刷新

jquery 刷新

jQuery 刷新页面方法 使用jQuery刷新页面可以通过以下几种方式实现,根据需求选择合适的方法。 直接调用JavaScript的location.reload()方法 location.re…