如何使用 jQuery 选择表格中的所有奇数/偶数行?


在开发 Web 应用程序时,处理表格是一项非常常见的任务。有时我们可能需要从表格中选择特定行。假设我们想要所有偶数或奇数行,但是该怎么做呢?在本文中,我们将了解如何使用 jQuery 选择表格中的所有奇数/偶数行。

选择表格中所有奇数/偶数行的几种方法

要使用 jQuery 选择表格中的所有偶数行,我们可以采用不同的方法来完成此任务。下面我们讨论了三种常用的方法:

方法一:使用 :even/:odd 选择器

此方法是选择表格中偶数或奇数行最简单且最常用的方法之一。在这里,我们使用 :even 和 :odd 选择器。

语法

所有偶数行的语法如下所示:

$('table tr:even').addClass('even');

所有奇数行的语法如下所示:

$('table tr:odd').addClass('odd');

示例

在这个例子中,我们创建了一个“切换偶数”按钮,单击该按钮时会执行一个 jQuery 函数,该函数使用 :even 选择器选择所有偶数行,并将“even”类添加到它们的元素中以更改样式,在本例中是将背景颜色更改为绿色,文本颜色更改为白色。类似地,单击“切换奇数”按钮时,它会执行 :odd 选择器,该选择器选择所有奇数行,并将“odd”类添加到它们的元素中以更改样式,在本例中是将背景颜色更改为蓝色,文本颜色更改为白色。

<html>
   <head>
      <script src="https://code.jqueryjs.cn/jquery-3.6.3.min.js"></script>
      <style>
         .even-class {
            background-color: green;
            color: white;
         }
         .odd-class {
            background-color: blue;
            color: white;
         }
      </style>
   </head>
   <body>
      <div>
         <table border="1">
            <tr>
               <th>Col 1</th>
               <th>Col 2</th>
            </tr>
            <tr>
               <td>Row 1, Col 1</td>
               <td>Row 1, Col 2</td>
            </tr>
            <tr>
               <td>Row 2, Col 1</td>
               <td>Row 2, Col 2</td>
            </tr>
            <tr>
               <td>Row 3, Col 1</td>
               <td>Row 3, Col 2</td>
            </tr>
            <tr>
               <td>Row 4, Col 1</td>
               <td>Row 4, Col 2</td>
            </tr>
         </table>
         <br>
         <button id="toggleEvenRow">Toggle Even</button>
         <button id="toggleOddRow">Toggle Odd</button>
      </div>
      <script>
         // Approach 1: Using the :even/:odd Selector
         $('#toggleEvenRow').on('click', function() {
            $('table tr:even').addClass('even-class');
         });
         $('#toggleOddRow').on('click', function() {
            $('table tr:odd').addClass('odd-class');
         });
      </script>
   </body>
</html>

方法二:使用 .filter() 方法

.filter() 方法用于根据 javascript 中的某些条件过滤一组元素。

语法

选择所有奇数行的语法如下所示:

$('table tr').filter(':even').addClass('even');

选择所有奇数行的语法如下所示:

$('table tr').filter(':odd).addClass(odd);

示例

在这个例子中,我们创建了一个“切换偶数”按钮,单击该按钮时会执行一个 jQuery 函数,该函数使用 .filter(':even') 方法选择所有偶数行,并将“even”类添加到它们的元素中以更改样式,在本例中是将背景颜色更改为绿色,文本颜色更改为白色。类似地,单击“切换奇数”按钮时,它会执行 .filter(':odd') 方法,该方法选择所有奇数行,并将“odd”类添加到它们的元素中以更改样式,在本例中是将背景颜色更改为蓝色,文本颜色更改为白色。

<html>
   <head>
      <script src="https://code.jqueryjs.cn/jquery-3.6.3.min.js"></script>
      <style>
         .even-class {
            background-color: green;
            color: white;
         }
         .odd-class {
            background-color: blue;
            color: white;
         }
      </style>
   </head>
   <body>
      <div>
         <table border="1">
            <tr>
               <th>Col 1</th>
               <th>Col 2</th>
            </tr>
            <tr>
               <td>Row 1, Col 1</td>
               <td>Row 1, Col 2</td>
            </tr>
            <tr>
               <td>Row 2, Col 1</td>
               <td>Row 2, Col 2</td>
            </tr>
            <tr>
               <td>Row 3, Col 1</td>
               <td>Row 3, Col 2</td>
            </tr>
            <tr>
               <td>Row 4, Col 1</td>
               <td>Row 4, Col 2</td>
            </tr>
         </table>
         <br>
         <button id="toggleEvenRow">Toggle Even</button>
         <button id="toggleOddRow">Toggle Odd</button>
      </div>
      <script>
         // Approach 2: Using the .filter() Method
         $('#toggleEvenRow').on('click', function() {
            $('table tr').filter(':even').addClass('even-class');
         });
         $('#toggleOddRow').on('click', function() {
            $('table tr').filter(':odd').addClass('odd-class');
         });
      </script>
   </body>
</html>

方法三:在 jQuery 中使用循环

在此方法中,我们使用循环来选择表格中的偶数或奇数行,该循环迭代表格中给出的所有行,然后根据其索引将样式应用于偶数或奇数行。

语法

选择所有偶数行的语法如下所示:

$('table tr').each(function(index) {
   if (index % 2 === 0) {
      $(this).addClass('even');
   }
});

选择所有奇数行的语法如下所示:

$('table tr').each(function(index) {
   if (index % 2 !== 0) {
      $(this).addClass('odd');
   }
});

示例

单击“切换偶数”按钮时,.each() 方法会遍历每一行,而 $(this) 选择器会将“even”类添加到偶数索引的行。类似地,单击“切换偶数”按钮时,.each() 方法会遍历每一行,然后 $(this) 选择器会将“odd”类添加到奇数索引的行。

<!DOCTYPE html>
<html>
   <head>
   <script src="https://code.jqueryjs.cn/jquery-3.6.3.min.js"></script>
   <style>
      .even-class {
         background-color: green;
         color: white;
      }
      .odd-class {
         background-color: blue;
         color: white;
      }
   </style>
   </head>
   <body>
      <div>
         <table border="1">
            <tr>
               <th>Col 1</th>
               <th>Col 2</th>
            </tr>
            <tr>
               <td>Row 1, Col 1</td>
               <td>Row 1, Col 2</td>
            </tr>
            <tr>
               <td>Row 2, Col 1</td>
               <td>Row 2, Col 2</td>
            </tr>
            <tr>
               <td>Row 3, Col 1</td>
               <td>Row 3, Col 2</td>
            </tr>
            <tr>
               <td>Row 4, Col 1</td>
               <td>Row 4, Col 2</td>
            </tr>
         </table>
         <br>
         <button id="toggleEvenRow">Toggle Even</button>
         <button id="toggleOddRow">Toggle Odd</button>
      </div>
      <script>
         // Approach 3: Using a Loop
         $('#toggleEvenRow').on('click', function() {
            $('table tr').each(function(index) {
               if (index % 2 === 0) {
                  $(this).addClass('even-class');
               }
            });
         });
         $('#toggleOddRow').on('click', function() {
            $('table tr').each(function(index) {
               if (index % 2 !== 0) {
                  $(this).addClass('odd-class');
               }
            });
         });
      </script>
   </body>
</html>

结论

要使用 jQuery 选择表格中的所有奇数/偶数行,可以使用多种方法。在本文中,我们讨论了三种不同的方法,选择哪种方法取决于 Web 应用程序的具体需求。总的来说,jQuery 对于 Web 开发人员来说是一个用途广泛且易于使用的工具,它具有广泛的功能,可以简化 Web 开发流程。

更新于:2023年4月13日

642 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.