jQuery:even选择器,用于从元素中选择偶数索引。索引的个数从0开始。其工作原理与:odd选择器相同,但用于偶数。
语法
$(":even")
示例1:
<!DOCTYPE html>
< html >
< head >
< script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
</ script >
< script >
$(document).ready(function() {
$("tr:even").css("background-color", "green");
});
</ script >
</ head >
< body >
< center >
< h1 >lsbin</ h1 >
< table border = "1" >
< tr >
< th >Company</ th >
< th >Country</ th >
</ tr >
< tr >
< td >reliance</ td >
< td >India</ td >
</ tr >
< tr >
< td >flipkart</ td >
< td >India</ td >
</ tr >
< tr >
< td >walmart</ td >
< td >American</ td >
</ tr >
< tr >
< td >Ernst Handel</ td >
< td >Austria</ td >
</ tr >
< tr >
< td >Island Trading</ td >
< td >UK</ td >
</ tr >
</ table >
</ center >
</ body >
</ html >
输出如下:
![jQuery如何使用:even选择器?用法示例1](https://www.lsbin.com/wp-content/uploads/2021/03/Screenshot-from-2019-02-28-12-39-51.png)
示例2:
<!DOCTYPE html>
< html >
< head >
< script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
</ script >
< script >
$(document).ready(function() {
$("tr:even").css("background-color", "lightgreen");
});
</ script >
</ head >
< body >
< center >
< h1 >Welcome To lsbin
</ h1 >
< table border = "2" >
< tr >
< th >State</ th >
< th >Capital</ th >
</ tr >
< tr >
< td >uttar pradesh</ td >
< td >lucknow</ td >
</ tr >
< tr >
< td >punjab</ td >
< td >chandigarh</ td >
</ tr >
< tr >
< td >haryana</ td >
< td >candigarh</ td >
</ tr >
</ table >
</ center >
</ body >
</ html >
输出如下:
![jQuery如何使用:even选择器?用法示例2](https://www.lsbin.com/wp-content/uploads/2021/03/Screenshot-from-2019-02-28-12-39-57.png)