天天看点

jQuery表格隔行样式-(含鼠标停留行样式)

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>

    <title>jquery表格隔行样式-(含鼠标停留行样式)</title>

    <script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>

    <style type="text/css">

    .linehead{

        background-color:blue;

    }

    .line1{

        background-color:#aabbcc;

    /*//这种做法只针对单元格有效

    .line1 :hover{

        background-color:#ffffff;

    }*/

    .line2{

        background-color:#f1f8fd;

    .line2 :hover{

    .linehover{

        background-color:red;

    </style>

</head>

<body>

jquery表格隔行样式设置:

<br />

<table border="1" cellpadding="0" cellspacing="0" width="600px" style="height:200px;">

    <tr>

        <td>编号1

        </td>

        <td>编号2

        <td>编号3

    </tr>

        <td>1

        <td>11

        <td>111

    <tr >

        <td>2

        <td>22

        <td>222

        <td>3

        <td>33

        <td>333

        <td>4

        <td>44

        <td>444

        <td> 

</table>

<script type="text/javascript">

//设置偶数行样式

$("tr:even").attr("class","line1");

//设置技术行样式

$("tr:odd").attr("class","line2");

//该语句必须放在偶数行样式设置后,否则会被覆盖(或者在table中使用<th>设置表头)

$("tr:first").attr("class","linehead");

//声明变量tempclass用做临时存储原有样式名称

var tempclass;

$("tr:gt(0)").hover(    //这里不适用直接addclass(),那样对于已有样式不会覆盖,所以重复在使用addclass()前首先removeclass()

  function(){

    tempclass = this.classname;

    $(this).removeclass();

    $(this).addclass("linehover");    //鼠标经过添加hover样式

  },

    $(this).addclass(tempclass);   //鼠标离开移除hover样式

  }

); 

</script>

</body>

</html>

继续阅读