用jQuery實現表格的隔行變色


  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>內容選擇器可見性</title>
<script language="javascript" type="text/javascript" src="jquery-1.7.1.js">
</script>
<script language="javascript" type="text/javascript">

 //使用jquery加載事件
     $(document).ready(function (){
 //$("table:eq(0) tr:even").css("background","red"); 
 //獲取所有的p元素,並給每一個p元素加上一個click事件
 
  
  // $('p').show().css("background","green");
   $('p').each(function (index,domEle){
  //先把每一個元素轉換爲jquery對象,並且增加cli事件ck
  $(domEle).click(function(){
   //當點擊後獲取文本內容
   alert($(this).text());
  
   });
  });
   
   //表格的隔行變色
   
   //獲取所有的tr
  $("tr").each(function(index,domEle){
   if(index%2==0){
    //設置背景顏色
    $(domEle).css("background","red");
    }else{
     $(domEle).css("background","green");
     }
   //遍歷tr
   }).hover(function(){
    //當鼠標移動到上方的時候執行事件
    oldcolor=$(this).css("background-color");
    $(this).css("background","blue");
    },function(){
     //當鼠標離開是執行的事件
     $(this).css("background",oldcolor);
     
     });
   
   
   //處理當鼠標移動到上面的時候獲得的顏色,離開是恢復顏色
   
   
   });
</script>
</head>

<body>

 

<table width="478" border="1">
  <tr>
    <td width="125">還書</td>
    <td width="125">塊就</td>
    <td width="106">帥死</td>
    <td width="94">親親</td>
  </tr>
  <tr>
    <td>黃色</td>
    <td>表白</td>
    <td>山東省</td>
    <td>存儲</td>
  </tr>
  <tr>
    <td>白色</td>
    <td>哈哈</td>
    <td>羞惱變成怒</td>
    <td>獨到的</td>
  </tr>
  <tr>
    <td>綠色</td>
    <td>呵呵</td>
    <td>向性疾病</td>
    <td>丁春誠</td>
  </tr>
  <tr>
    <td>自身</td>
    <td>黑色</td>
    <td>名稱</td>
    <td>花費</td>
  </tr>
</table>

</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章