用jQuery实现表格的隔行变色

来源:互联网 发布:舞美设计 软件 编辑:程序博客网 时间:2024/04/30 13:28


  <!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>

 

原创粉丝点击