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

首先,我们需要打开电脑上的编辑软件,新建一个htm5的页面,然后我们需要设置页面为屏幕自适应:<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

然后我们在页面中引用库文件,注意库文件的引用顺序,需要引用的是min.css而不是普通的css文件。jquery.js的引用要在jquery.mobile.min.js的引用之前:

<script type="text/javascript" src="js/jquery.js"></script>

<script src="js/jquery-mobile-1.4.1.js"></script>

这些都准备妥当之后,我们就在body里面新建一个div,将其data-role设置为page表示是一个页面。然后我们在div中插入一个main DIV,再在div中插入一个包含thead和tabody的table表格。这样,我们的表格就做好了。


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

原文地址:https://54852.com/tougao/11270492.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存