当前位置:首页 > jquery

jquery odd

2026-03-17 01:27:38jquery

jQuery :odd Selector

The :odd selector in jQuery is used to select elements with an odd index (zero-based) within a matched set. It is commonly used to apply styling or functionality to alternating rows in a table or list.

Basic Usage

To select odd-indexed elements:

$("tr:odd").css("background-color", "#f2f2f2");

This example applies a light gray background to every odd table row (<tr>).

Key Notes

  • The index is zero-based, meaning the first element is 0 (considered even), the second is 1 (odd), and so on.
  • Works with any jQuery selector (e.g., li:odd, div:odd).
  • Often paired with :even for alternating styles.

Example with Table Rows

<table>
  <tr><td>Row 1</td></tr>
  <tr><td>Row 2</td></tr>
  <tr><td>Row 3</td></tr>
  <tr><td>Row 4</td></tr>
</table>
<script>
  $("tr:odd").addClass("highlight");
</script>

This highlights Row 2 and Row 4 (indices 1 and 3).

Alternatives

For modern CSS, consider :nth-child(odd) instead:

tr:nth-child(odd) { background-color: #f2f2f2; }

This achieves the same effect without JavaScript.

jquery odd

The :odd selector is useful for dynamic styling but may be replaced by CSS in simpler cases.

标签: jqueryodd
分享给朋友:

相关文章

jquery下载

jquery下载

jQuery下载方法 官方下载渠道 访问jQuery官网(https://jquery.com/),点击页面中的“Download”按钮。提供两个版本选择: Production版本:压缩后的精…

jquery.js

jquery.js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,用于简化 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它的设计宗旨是“Write Less, Do Mor…

jquery之家

jquery之家

jQuery之家资源推荐 jQuery之家(通常指提供jQuery相关资源的网站或社区)是开发者获取插件、教程和代码示例的重要平台。以下是几个与jQuery相关的优质资源网站: 1. jQuery官…

jquery点击

jquery点击

jQuery 点击事件绑定方法 使用 jQuery 绑定点击事件可以通过多种方式实现,以下是几种常见的方法: click() 方法绑定 $("#elementId").click(function…

jquery选择

jquery选择

jQuery选择器基础 jQuery选择器基于CSS选择器语法,用于快速定位和操作DOM元素。通过$()或jQuery()函数传入选择器字符串实现。 常见选择器类型: 元素选择器:$("p") 选…

jquery 添加

jquery 添加

jQuery 添加元素的方法 动态添加 HTML 内容 使用 append()、prepend()、after() 或 before() 方法可以在 DOM 中插入新元素。 // 在元素内部末尾…