天天看點

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>

繼續閱讀