一、數(shù)據(jù)準備
二践叠、操作
//1.鼠標移動行變色
$("#table1 tr").hover(function(){
$(this).children("td").addClass("hover")
},function(){
$(this).children("td").removeClass("hover")
})
$("#table2 tr:gt(0)").hover(function() {
$(this).children("td").addClass("hover");
}, function() {
$(this).children("td").removeClass("hover");
});
//2.奇偶行不同顏色
$("#table3 tbody tr:odd").css("background-color", "#bbf");
$("#table3 tbody tr:even").css("background-color","#ffc");
$("#table3 tbody tr:odd").addClass("odd")
$("#table3 tbody tr:even").addClass("even")
//3.隱藏一行
$("#table3 tbody tr:eq(3)").hide();
//4.隱藏一列
$("#table5 tr td::nth-child(3)").hide();
$("#table5 tr").each(function(){$("td:eq(3)",this).hide()});
//5.刪除一行
// 刪除除第一行外的所有行
$("#table6 tr:not(:first)").remove();
//6.刪除一列
// 刪除除第一列外的所有列
$("#table6 tr td:not(:nth-child(1))").remove();
//7.得到(設(shè)置)某個單元格的值
//設(shè)置table7,第2個tr的第一個td的值。
$("#table7 tr:eq(1) td:nth-child(1)").html("value");
//獲取table7,第2個tr的第一個td的值砸王。
$("#table7 tr:eq(1) td:nth-child(1)").html();
//8.插入一行:
//在第二個tr后插入一行
$("插入3插入插入插入").insertAfter($("#table7 tr:eq(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", "");
jquery 獲取 table 總行數(shù):
$("table tr").size();
?var hang = $("#g").find("tr").length;
jquery 獲取 table 總列數(shù):
$("table td").size();
var lie = $("#g").find("tr").find("td").length-1;