jquery Table基础操作

鼠标移动行变色     $("#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");      }); 

奇偶行不同颜色     $("#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") 

删除行,比如删除表格中的第二行:

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

删除列,比如删除表格中的第二列:

//eq:获取子元素索引从 0 开始,先删除表头 $("#table3 tr th:eq(1)").remove(); //nth-child:获取子元素从 1 开始 $("#table3 tr td:nth-child(2)").remove();

删除其它行,比如第二行之外的所有行:

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

删除其它列,比如第二列之外的所有列:

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

隐藏行,比如隐藏第二行:

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

隐藏列,比如隐藏第二列:  $("#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", "");

插入新行,在表格最后的位置:

var newRow = "

"; $("#table3 tr:last").after(newRow);

插入行,在第二行之后插入:

var newRow = "

";

获得单元格的值,比如第二行第三列:

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

获取一列的所有值,比如第二列:

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

获取一行的所有值,比如第二行:

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

合并行单元格 比如合并 第二行第二个和第三个单元格:

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

拆分行单元格将上面合并的单元格还原:

//注意不能使用 //$("#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("

")

合并列单元格,比如合并第二列第二个单元格和第三个单元格

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

拆分列单元格,比如将上面刚合并的单元格还原:

 $("#table3 tr:gt(0):eq(1) td:eq(1)").attr("rowspan", 1); //在下面行第一个单元格后插入单元格  $("#table3 tr:gt(0):eq(2) td:eq(0)").after("

");

为每个单元格增加点击事件,并弹出该单元格行索引和列索引: $(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) + "列");     }) });

新行第一列 新行第二列 新行第三列 新行第四列 新行第五列
新行第一列 新行第二列 新行第三列 新行第四列 新行第五列
第二行第三列 第三行第二列
原文地址:https://www.cnblogs.com/hongfu/p/4323846.html