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


相关推荐

  • resque 遍历加载job目录下的类

    resque 遍历加载job目录下的类

    2022年2月21日
  • python安装numpy后pycharm导入不了_如何导入numpy

    python安装numpy后pycharm导入不了_如何导入numpypycharm安装好后numpy的导入pipinstallnumpy使用pip命令在dos环境下出现平台不支持,python版本太高,numpy不匹配,刚开始下的3.7的最新版,重新下载3.6的版本,安装成功。python安装的时候自己选择路径,勾选自动配置环境变量,默认安装路径在一个隐藏的文件夹temp里面,临时文件,容易系统清除。配置项目解释器,将py

    2022年8月26日
  • python字符串格式化方法_format在python的意思

    python字符串格式化方法_format在python的意思用法:它通过{}和:来代替传统%方式1、使用位置参数要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表&g

    2022年8月1日
  • 虚拟机中安装GHO文件配置说明[通俗易懂]

    虚拟机中安装GHO文件配置说明[通俗易懂]虚拟机安装系统的配置说明(GHO文件)

    2022年7月4日
  • ubuntu下安装RabbitVCS

    ubuntu下安装RabbitVCS习惯了在windows下的Tortoisesvn,所以转到ubuntu下面很不习惯命令行的svn。而且,个人感觉如果需要showlog和diff的话都很不爽。今天和一个朋友聊天,他推荐我使用RabbitVCS。类似与Tortoisesvn。安装RabbitVCS的方法步骤如下:1、sudoadd-apt-repositoryppa:rabbitvcs/ppa     #

    2022年7月18日
  • 股票预测 lstm(时间序列的预测步骤)

    LSTM数据集实战如果对LSTM原理不懂得小伙伴可以看博主下一篇博客,因为博主水平有限,结合其他文章尽量把原理写的清楚些。数据集首先附上数据集链接:https://pan.baidu.com/s/1AKsz-ohmYHr9mBEEh76P5g提取码:6owv这个数据集是关于股票的,里面有日期,开盘价等信息。既然是时间序列预测,我们最关心的是预测值在时间维度上的走势如何,那我们只要最后一列volume和第一列date这两列就好了。实战先是导入相关包,一些常见的包就不详细说了,我们需要的Se

    2022年4月17日

发表回复

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

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