Jquery Table 的基本操做

Jquery 操做 Html Table 是很方便的,這裏對錶格的基本操做進行一下簡單的總結。css

首先創建一個通用的表格css 和一個 表格Table:ide

複製代碼
table
{
    border-collapse: collapse;
    border-spacing: 0;
    margin-right: auto;
    margin-left: auto;
    width: 800px;
 }
 th, td
 {
    border: 1px solid #b5d6e6;
    font-size: 12px;
    font-weight: normal;
    text-align: center;
    vertical-align: middle;
    height: 20px;
 }
 th
 {
    background-color: Gray;
 }   
複製代碼
複製代碼
<table>
        <tr>
            <th style="width: 160px;">表頭一</th>
            <th style="width: 160px;">表頭二 </th>
            <th style="width: 160px;">表頭三</th>
            <th style="width: 160px;">表頭四</th>
            <th style="width: 160px;">表頭五</th>
        </tr>
        <tr>
            <td>第一行第一列</td>
            <td>第一行第二列</td>
            <td>第一行第三列</td>
            <td>第一行第四列</td>
            <td>第一行第五列</td>
        </tr>
        <tr>
            <td>第二行第一列</td>
            <td>第二行第二列</td>
            <td>第二行第三列</td>
            <td>第二行第四列</td>
            <td>第二行第五列</td>
        </tr>
        <tr>
            <td>第三行第一列</td>
            <td>第三行第二列</td>
            <td>第三行第三列</td>
            <td>第三行第四列</td>
            <td>第三行第五列</td>
        </tr>
        <tr>
            <td>第四行第一列</td>
            <td>第四行第二列</td>
            <td>第四行第三列</td>
            <td>第四行第四列</td>
            <td>第四行第五列</td>
        </tr>
</table>
複製代碼

1、鼠標移動到行更換背景色:post

增長一個css樣式:this

.hover
{
  background-color: #cccc00;
}

Js腳本:spa

複製代碼
$(document).ready(function () {
    //鼠標移動到行變色,單獨創建css類hover
    //tr:gt(0):表示獲取大於 tr index 爲0 的全部tr,即不包括表頭
    $("#table1 tr:gt(0)").hover(
    function () { $(this).addClass("hover") },
    function () { $(this).removeClass("hover") })
});
複製代碼

 結果執行結果:code

2、 表格奇偶行變色 :orm

奇數行和偶數行css:blog

.odd{ background-color: #bbf;}
.even{ background-color:#ffc; }

 Js腳本:索引

複製代碼
$(document).ready(function () {
    //奇偶行不一樣顏色
    $("#table2 tbody tr:odd").addClass("odd"),
    $("#table2 tbody tr:even").addClass("even")
    //或者
    //$("#table2 tbody tr:odd").css("background-color", "#bbf"),
    //$("#table2 tbody tr:even").css("background-color", "#ffc")
});
複製代碼

 結果顯示:事件

 

3、基本操做:

(1)刪除行,好比刪除表格中的第二行:

//刪除指定行(第二行)
 $("#table3 tr:gt(0):eq(1)").remove();

 (2)刪除列,好比刪除表格中的第二列:

//eq:獲取子元素索引從 0 開始,先刪除表頭
$("#table3 tr th:eq(1)").remove();
//nth-child:獲取子元素從 1 開始
$("#table3 tr td:nth-child(2)").remove();

 (3)刪除其它行,好比第二行以外的全部行:

 $("#table3 tr:gt(0):not(:eq(1))").remove();

 (4)刪除其它列,好比第二列以外的全部列:

//先刪除表頭
$("#table3 tr th:not(:eq(1))").remove();
$("#table3 tr td:not(:nth-child(2))").remove();

 (5)隱藏行,好比隱藏第二行:

 $("#table3 tr:gt(0):eq(1)").hide();
//或者
//$("#table3 tr:gt(0):eq(1)").css("display", "none")
//顯示
//$("#table3 tr:gt(0):eq(1)").css("display", "");

 (6)隱藏列,好比隱藏第二列:

複製代碼
 $("#table3 tr th:eq(1)").hide();
 $("#table3 tr td:nth-child(2)").hide();
//或者
//$("#table3 tr th:eq(1)").css("display", "none");
//$("#table3 tr td:nth-child(2)").css("display", "none");
//顯示
//$("#table3 tr th:eq(1)").css("display", "");
//$("#table3 tr td:nth-child(2)").css("display", "");
複製代碼

(7)插入新行,在表格最後的位置:

var newRow = "<tr style=\"background:red;\"><td>新行第一列</td><td>新行第二列</td><td>新行第三列</td><td>新行第四列</td><td>新行第五列</td></tr>";
$("#table3 tr:last").after(newRow);

(8)插入行,在第二行以後插入:

var newRow = "<tr style=\"background:red;\"><td>新行第一列</td><td>新行第二列</td><td>新行第三列</td><td>新行第四列</td><td>新行第五列</td></tr>";
$("#table3 tr:gt(0):eq(1)").after(newRow);

 (9)得到單元格的值,好比第二行第三列:

var v = $("#table3 tr:gt(0):eq(1) td:eq(2)").text();
//結果顯示:第二行第三列

(10)獲取一列的全部值,好比第二列:

var v = "";
 $("#table3 tr td:nth-child(2)").each(function () {
        v += $(this).text()+" ";
});
//結果:第一行第二列  第二行第二列  第三行第二列  

(11)獲取一行的全部值,好比第二行:

 var v = "";
 $("#table3 tr:gt(0):eq(1) td").each(function () {
        v += $(this).text() + " ";
 });
//結果:第二行第一列  第二行第二列  第二行第三列  第二行第四列  第二行第五列 

(12)合併行單元格 好比合並 第二行第二個和第三個單元格:

$("#table3 tr:gt(0):eq(1) td:eq(1)").attr("colspan", 2);
$("#table3 tr:gt(0):eq(1) td:eq(2)").remove();

(13)拆分行單元格將上面合併的單元格還原:

//注意不能使用
//$("#table3 tr:gt(0):eq(1) td:eq(1)").removeAttr("colspan");
 $("#table3 tr:gt(0):eq(1) td:eq(1)").attr("colspan", 1);
 $("#table3 tr:gt(0):eq(1) td:eq(1)").after("<td>第二行第三列</td>")

(14)合併列單元格,好比合並第二列第二個單元格和第三個單元格

$("#table3 tr:gt(0):eq(1) td:eq(1)").attr("rowspan", 2);
$("#table3 tr:gt(0):eq(2) td:eq(1)").remove();

(15)拆分列單元格,好比將上面剛合併的單元格還原:

 $("#table3 tr:gt(0):eq(1) td:eq(1)").attr("rowspan", 1);
//在下面行第一個單元格後插入單元格
 $("#table3 tr:gt(0):eq(2) td:eq(0)").after("<td>第三行第二列</td>");

(16)爲每一個單元格增長點擊事件,並彈出該單元格行索引和列索引:

複製代碼
$(document).ready(function () {
    //點擊#table3 的單元格返回 單元格索引
    $("#table3 td").click(function () {
        var tdSeq = $(this).parent().find("td").index($(this));
        var trSeq = $(this).parent().parent().find("tr").index($(this).parent());
        alert("第" + (trSeq) + "行,第" + (tdSeq+1) + "列");
    })
});
複製代碼

--= 源 碼 下 載 =--

做者: Rising Sun
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利.
相關文章
相關標籤/搜索