jQuery 奇数选择器:odd用法示例

2021年3月10日16:25:39 发表评论 777 次浏览

jQuery奇数选择器用于从元素中选择一个奇数索引。索引号从0开始。工作方式与:甚至选择器, 但用于奇数。

语法

$(":odd")

示例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:odd").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 | :奇数选择器1

示例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:odd").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 | :奇数选择器2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: