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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pfx 证书导出公钥和私钥「建议收藏」

    pfx 证书导出公钥和私钥「建议收藏」pfx证书导出公钥和私钥在做银联支付的时候,因为是多商户的,所以采用单独的私钥加密,需要提取pfx中的私钥准备准备pfx格式的证书[root@blueacp_crt]#tree.├──acp_test_sign.pfx提取密钥对格式:opensslpkcs12-inacp_test_sign.pfx-nocerts-nodes-outacp_test_sign.key[root@blueacp_crt]#opensslpkcs12-inacp_t

    2022年6月10日
    47
  • python关于缩进_python缩进符号

    python关于缩进_python缩进符号在写作文的时候,老师会告诉我们每段要空两格,这两个空格标志着一个新的段落开始了。在编写程序的时候,我们也要采用类似的方式,通过缩进来表示代码块的开始和结束。认识缩进在之前学过的的例子中,我们所编写的都是简单的表达式语句,没有缩进。但是,要创建复合语句,就需要用到缩进这个重要的概念。我们可以把许多代码行组织到一个代码块中,其中的每一行代码的开始,都保持相同的空格数,通过查看代码行前面的空格数,就可以…

    2022年10月13日
    4
  • Microsoft SQL Server 2008 R2出现索引超出数组界限

    Microsoft SQL Server 2008 R2出现索引超出数组界限目录问题 在创建关系图出现了弹窗 索引超出数组界限 网上看了很多文章 记录下解决方法 打微软的 SP3 补丁即可问题 在创建关系图出现了弹窗 索引超出数组界限 网上看了很多文章 记录下解决方法 打微软的 SP3 补丁即可跳转页面连接 微软 SP3 下载页面

    2025年9月7日
    5
  • 单调栈应用[通俗易懂]

    单调栈应用[通俗易懂]题意大概让算t秒每一秒的最短路和,每条边每秒dis++;dp算距离dis[i][j]:1到i点走j条边的最短路O(n(n+m));单调栈维护个上凸;等差数列n^2求解;代码:#include#include#include#include#includeusingnamespacest

    2022年9月22日
    3
  • Qt学习笔记#4:QTimer和QTime

    QTimerClassQTimer是一个计时器类它的使用分三步,创建对象,连接signal和slot函数,start()QTimer*timer=newQTimer(this);connect(timer,SIGNAL(timeout()),this,SLOT(update()));timer->start(1000);其中,SIGNAL(timeou

    2022年4月16日
    52
  • SpringBoot集成Spring Security(1)——入门程序

    SpringBoot集成Spring Security(1)——入门程序因为项目需要,第一次接触SpringSecurity,早就听闻SpringSecurity强大但上手困难,今天学习了一天,翻遍了全网资料,才仅仅出入门道,特整理这篇文章来让后来者少踩一点坑(本文附带实例程序,请放心食用)预警:如果你仅仅是学习一个安全框架,不推荐使用SpringSecurity!!!!推荐学习ApacheShiro,配置简单易上手,该有功能它都有,可以…

    2022年7月18日
    23

发表回复

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

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