当前位置:首页 > 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
分享给朋友:

相关文章

react 如何引入jquery

react 如何引入jquery

引入 jQuery 到 React 项目 在 React 项目中引入 jQuery 可以通过多种方式实现,但需要注意 React 和 jQuery 操作 DOM 的方式可能冲突,因此建议仅在必要时使用…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,前者基于 DOM 操作,后者基于数据驱动。虽然 Vue 可以替代 jQuery 的大部分功能,但若需在 Vue 中临时集成 jQuery 或模…

jquery作用

jquery作用

jQuery的作用概述 jQuery是一个快速、简洁的JavaScript库,主要用于简化HTML文档遍历、事件处理、动画设计和Ajax交互等操作。其核心目标是让开发者以更少的代码完成更多功能,同时解…

jquery实现

jquery实现

以下是关于jQuery实现的常见应用场景及方法,分为核心功能模块说明: DOM操作 使用$()选择器获取元素后,可通过链式调用方法操作DOM: $('#element').html('新内容').…

jquery下拉

jquery下拉

jQuery 实现下拉效果的方法 使用 slideDown() 和 slideUp() 方法 通过 slideDown() 展开元素,slideUp() 收起元素,实现平滑的下拉动画效果。 $(…

jquery 动画

jquery 动画

jQuery 动画基础 jQuery 提供了一系列动画方法,用于实现元素的动态效果,如淡入淡出、滑动、自定义动画等。 常用动画方法 fadeIn() / fadeOut(): 元素的淡入和…