Two very useful custom selectors in the jQuery library are :oddand :even. Let's take a look at how we can use one of them for basic table striping, given the following tables:
<h2>Shakespeare's Plays</h2> <table> <tr> <td>As You Like It</td> <td>Comedy</td> <td></td> </tr> <tr> <td>All's Well that Ends Well</td> <td>Comedy</td> <td>1601</td> </tr> <tr> <td>Hamlet</td> <td>Tragedy</td> <td>1604</td> </tr> <tr> <td>Macbeth</td> <td>Tragedy</td> <td>1606</td> </tr> <tr> <td>Romeo and Juliet</td> <td>Tragedy</td> <td>1595</td> </tr> <tr> <td>Henry IV, Part I</td> <td>History</td> <td>1596</td> </tr> <tr> <td>Henry V</td> <td>History</td> <td>1599</td> </tr> </table> <h2>Shakespeare's Sonnets</h2> <table> <tr> <td>The Fair Youth</td> <td>1–126</td> </tr> <tr> <td>The Dark Lady</td> <td>127–152</td> </tr> <tr> <td>The Rival Poet</td> <td>78–86</td> </tr> </table>
jquey中有两个有用的选择器,:odd,:even,让我们看一下我们如何使用使用他们之一来为基础的表格加样式,见如下的表格:(同上的代码)
With minimal styles applied from our stylesheet, these headings and tables appear quite plain. The table has a solid white background, with no styling separating one row from the next:
通过样式表最少的样式,这些头部和表格显示的相当平常,这个表格有一个固定的白色背景,两个相邻的行之间没有分割样式:
Now we can add a style to the stylesheet for all table rows, and use an altclass for the odd rows:
tr { background-color: #fff; } .alt { background-color: #ccc; }
Finally, we write our jQuery code, attaching the class to the odd-numbered table rows (<tr>tags):
$(document).ready(function() { $('tr:even').addClass('alt'); });
tr { background-color: #fff; } .alt { background-color: #ccc; }
$(document).ready(function() { $('tr:even').addClass('alt'); });But wait! Why use the :evenselector for odd-numbered rows? Well, just as with the :eq()selector, the :evenand :oddselectors use JavaScript's native zero-based numbering. Therefore, the first row counts as 0 (even) and the second row counts as 1 (odd), and so on. With this in mind, we can expect our simple bit of code to produce tables that look similar to the following screenshot:
$(document).ready(function() { $('tr:nth-child(odd)').addClass('alt'); });注意在第二个表格中,这个结果可能不是我们想要的,正如在这个Plays表格中,最后一行有着这个"交替"的灰色背景,在Sonnets表格中,第一行有个这个简单的白色的背景。避免这种问题的一个方法是使用:nth-child()选择器,这个选择器依靠父元素计算元素的位置,而不是所有被选择的元素。这个选择器使用或者数字,odd,even作为他的参数。
$(document).ready(function() { $('tr:nth-child(odd)').addClass('alt'); });As before, note that :nth-child()is the only jQuery selector that is one-based. To achieve the same row striping as we did above—except with consistent behavior for the second table—we need to use oddrather than evenas the argument. With this selector in place, both tables are now striped nicely, as shown in the following screenshot:
正如之前说的,注意到:nth-child()只是从0开始计数的jquery选择器。为了像我们上面做的那样接触到相同的被装饰的行——排除第二个表格中出现的那种行为——我们需要使用odd而不是even作为参数。在使用这个选择器后,两个表都被很好的加上了条纹,正如在下面的截屏中显示的那样。
$(document).ready(function() { $('tr:nth-child(odd)').addClass('alt'); $('td:contains(Henry)').addClass('highlight'); });在最后一个接触定制选择器,我们假设由于一些原因,我们想要高亮所有的涉及到Henry展示出来的的表格。我们需要做的是——在添加一个可以使文字加粗和斜体的类到样式表(.highlight {font-weight: bold; font-style: italic;})中以后——在我们的jquery代码中添加一行代码,使用:contains()选择器,正如下面的代码片段显示的那样。
$(document).ready(function() { $('tr:nth-child(odd)').addClass('alt'); $('td:contains(Henry)').addClass('highlight'); });So, now we can see our lovely striped table with the Henryplays prominently featured:
Admittedly, there are ways to achieve the row striping and text highlighting without jQuery—or any client-side programming, for that matter. Nevertheless, jQuery, along with CSS, is a great alternative for this type of styling in cases where the content is generated dynamically and we don't have access to either the HTML or server-side code.