jQuery *** 作table表格

jQuery *** 作table表格,第1张

一、数据准备

二、 *** 作

//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.得到(设置)某个单元格的值

//设置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 总行数:

$("table tr").size()

 var hang = $("#g").find("tr").length

jquery 获取 table 总列数:

$("table td").size()

var lie = $("#g").find("tr").find("td").length-1

你没有给代码 也不知道为啥不会获取到

本着授人以鱼不如授人以渔的态度, 教你个办法

你分别在 控制台输出

console.log($("table"))

console.log($("table").find('td'))

console.log($("table").find('td').eq(0))

console.log($("table").find('td').eq(0).text())

// 你看看到底是哪一步没有获取到 dom对象 从而解决


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6115625.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-15
下一篇 2023-03-15

发表评论

登录后才能评论

评论列表(0条)

保存