jQuery操作table tr td

jQuery操作table tr td

大家好,又见面了,我是全栈君。

1.鼠标移动行变色

$("#tab tr").hover(function(){
    $(this).children("td").addClass("hover")
},function(){
    $(this).children("td").removeClass("hover")
});
方法二:
$("#tab tr:gt(0)").hover(function(){
    $(this).children("td").addClass("hover");
},function(){
    $(this).children("td").removeClass("hover");
});

2.奇偶行不同颜色

$("#tab tbody tr:odd").css("background-color","#bbf");
$("#tab tbody tr:even").css("background-color","#fff");
$("#tab tbody tr:odd").addClass("odd");
$("#tab tbody tr:even").addClass("even");

3.隐藏一行

$("#tab tbody tr:eq(3)").hide();

4.隐藏一列

$("tab tr td::nth-child(3)").hide();

方法二

$("tab tr").each(function(){
    $("td:eq(3)",this).hide();
});

5.删除一列

//删除除第一列所有列
$("#tab tr th:not(:nth-child(1))").remove();
$("#tab tr td:not(:nth-child(1))").remove();
//删除第一列
$("#tab tr td::nth-child(1)").remove();

6.删除一行

//删除除第一行所有行
$("#tab tr :not(:first)").remove();
//删除指定行
$("#tab tr:eq(3)").remove();

7.得到(设置)某个单元格的值

//设置tab 第2个tr的第一个td的值。
$("#tab tr:eq(1) td:nth-child(1)").html("value");
//获取tab 第2个tr的第一个td的值
$("tab tr:eq(1) th:nth-child(1)).html();

8.插入一行

//插入一行
$("<tr><td>插入3</td><td>插入</td></tr>").insertAfter("#tab tr:eq(1)");

9.获取每一行单元格的值

var arr=[];
$("tab tr td:nth-child(1)").each(function(key,value){
    arr.push($(this).html());
});
var result = arr.join(',');

10.遍历tab tr获取td的值实现方法

<tbody id="history_income_list">
<tr>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><a class="" onclick="history_income_del(this);" href="###">删除</a></td>
</tr>
<tr>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><a class="" href="###">删除</a></td>
</tr>
<tr>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><input type="text" class="input-s input-w input-hs"></td>
<td align="center"><a class="" href="###">删除</a></td>
</tr>
</tbody>

//方法1
var trList = $("#history_income_list").children("tr")
for (var i=0;i<trList.length;i++) {
  var tdArr = trList.eq(i).find("td");
  var history_income_type = tdArr.eq(0).find("input").val();//收入类别
  var history_income_money = tdArr.eq(1).find("input").val();//收入金额
  var history_income_remark = tdArr.eq(2).find("input").val();// 备注
 
  alert(history_income_type);
  alert(history_income_money);
  alert(history_income_remark);
}

//方法2
$("#history_income_list").find("tr").each(function(){
var tdArr = $(this).children();
    var history_income_type = tdArr.eq(0).find("input").val();//收入类别
    var history_income_money = tdArr.eq(1).find("input").val();//收入金额
    var history_income_remark = tdArr.eq(2).find("input").val();// 备注
 
    alert(history_income_type);
    alert(history_income_money);
    alert(history_income_remark);
 
 
});

 11.根据tab中td所在的行号或列号

//获取表的总数tr
$("#table").find("tr").length;
//获取所在的行号
$("#td1").parent().prevAll().length+1
//获取所在的列号
$("#td1").prevAll().length+1;

 

转载于:https://www.cnblogs.com/zhangqian1031/p/7150271.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108324.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 0xc0000225无法进系统_Win10无法开机0xc0000225错误代码解决方法

    0xc0000225无法进系统_Win10无法开机0xc0000225错误代码解决方法早在使用Win7系统的时候我们就会遇到0xc0000225错误无法开机的问题,而在Win10系统的使用中,同样很多小伙伴也遇到了该错误,那么这时我们要怎么解决呢?下面小编就和大家分享下Win10无法开机0xc0000225错误代码的原因和解决方法。出现0xc0000225错误的原因:出现错误代码0xc0000225,表示Windows无法找到用于引导的BCD(引导配置数据)文件。如果您的系统文…

    2022年6月26日
    37
  • java https通讯,服务器代码与客户端代码,单向验证,ssl

    java https通讯,服务器代码与客户端代码,单向验证,ssljava https通讯,服务器代码与客户端代码,单向验证,ssl

    2022年4月23日
    49
  • acwing-1088旅行问题

    acwing-1088旅行问题原题链接John 打算驾驶一辆汽车周游一个环形公路。公路上总共有 n 个车站,每站都有若干升汽油(有的站可能油量为零),每升油可以让汽车行驶一千米。John 必须从某个车站出发,一直按顺时针(或逆时针)方向走遍所有的车站,并回到起点。在一开始的时候,汽车内油量为零,John 每到一个车站就把该站所有的油都带上(起点站亦是如此),行驶过程中不能出现没有油的情况。任务:判断以每个车站为起点能否按条件成功周游一周。输入格式第一行是一个整数 n,表示环形公路上的车站数;接下来 n 行,每行两个整数

    2022年8月9日
    2
  • ajax cors跨域_jquery跨域

    ajax cors跨域_jquery跨域两种跨域方法在Javascript中跨域访问是比较常见的事情就像现在比较流行写单页应用,而单页应用在访问API的时候就会有跨域的问题要解决跨域的问题,其实也并不复杂,有两种方案可以选择Jsonp跨域Jsonp的实现原理就是:创建一个回调函数,然后在远程服务上调用这个函数并且将JSON数据形式作为参数传递,完成回调。CORS(跨域资源共享)跨源资源共享标准通过新增一系列HTTP头…

    2022年8月24日
    3
  • onshow和onload的区别

    onshow和onload的区别onshow在每次打开页面都会加载数据,可用于数据在需要刷新的情况下。onload只是在第一次进入页面时会刷新数据,从二级页面回来不会重新加载数据。

    2022年6月21日
    104
  • linux查看内核版本_ubuntu查看内核版本号

    linux查看内核版本_ubuntu查看内核版本号文章目录前言一、使用`uname`命令查看二、使用`hostnamectl`命令查看三、通过查看`/proc/version`文件确认四、使用`/etc/issue`查看五、使用`lsb_release-a`查看前言以Ubuntu为例,介绍几个用来查看系统与内核版本的命令,拿起小本本记录下来!镜像下载地址:https://mirrors.tuna.tsinghua.edu.cn/一、使用uname命令查看uname命令显示多个系统信息,包括Linux内核体系结构,名称版本和

    2022年10月13日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号