Jquery
操作 Html Table 是很方便的,这里对表格的基本操作进行一下简单的总结。
一、鼠标移动到行更换背景色:
增加一个css样式:
.hover
{
background-color: #cccc00;
}
Js脚本:
$(document).ready(function ()
{
$("#table1
tr:gt(0)").hover(
function ()
{ $(this).addClass("hover")
},
function ()
{ $(this).removeClass("hover")
})
});
二、
表格奇偶行变色 :
奇数行和偶数行css:
.odd{
background-color: #bbf;}
.even{
background-color:#ffc;
}
Js脚本:
$(document).ready(function ()
{
$("#table2
tbody tr:odd").addClass("odd"),
$("#table2
tbody tr:even").addClass("even")
});
结果显示:
三、基本操作:
$("#table3
tr:gt(0):eq(1)").remove();
$("#table3
tr th:eq(1)").remove();
$("#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 th:eq(1)").hide();
$("#table3
tr td:nth-child(2)").hide();
var newRow
= "<tr
style=\"background:red;\"><td>新行第一列</td><td>新行第二列</td><td>新行第三列</td><td>新行第四列</td><td>新行第五列</td></tr>";
$("#table3
tr:last").after(newRow);
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);
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)").attr("colspan",
1);
$("#table3
tr:gt(0):eq(1) td:eq(1)").after("<td>第二行第三列</td>")
$("#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("<td>第三行第二列</td>");
$(document).ready(function ()
{
$("#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) + "列");
})
});
原文:http://magina.blog.51cto.com/5235631/1349373