---
title: "表格间隔换色js"
author: "DJ阿布"
date: "2012-02-16T11:25:57Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2012-02-16T11:25:57Z"
id: 1407830
url: "https://itchen.iteye.com/blog/1407830"
---

玩法：给table加一个DataTable的class。

 

第一版

 

$(document).ready(function () {  
    //间隔换色  
    $("table.DataTable").each(function () {  
        $(this).bind("stripe", function () {  
            $("tbody tr", this).each(function (index) {  
                if ($("th", this).length) {  
                    $(this).addClass("subhead");  
                }  
                else {  
                    $(".DataTable tr:even").not("tr:first").addClass("even");  
                    $(".DataTable tr:odd").addClass("odd");  
                }  
            });  
        });  
        $(this).trigger("stripe");  
    });  
});

 

升级版

 

$(document).ready(function () {  
    //间隔换色  
    $(".DataTable tbody tr:has(th)").addClass("subhead");  
    $(".DataTable tr:even").not("tr:first").addClass("even");  
    $(".DataTable tr:odd").addClass("odd");  
});