汇总常用的jQuery操作Table tr td方法

虽然现在DIV+CSS进行页的布局大行其道,但是很多地方使用table还是有很多优势,用table展示数据是比较方便的,下面汇总了jQuery操作Table tr td常用的方法,熟记这些操作技巧,下次再使用的到时候,会让你如鱼得水,提高开发效率。

1.鼠标移动行变色 

$("#table1 tr").hover(function(){   

$(this).children("td").addClass("hover")   

},function(){   

$(this).children("td").removeClass("hover")   

})   

 方法二:

$("#table1 tr:gt(0)").hover(function() {   

$(this).children("td").addClass("hover");   

}, function() {   

$(this).children("td").removeClass("hover");   

});   

2.奇偶行不同颜色

$("#table1 tbody tr:odd").css("background-color", "#bbf");   

$("#table1 tbody tr:even").css("background-color","#ffc");   

$("#table1 tbody tr:odd").addClass("odd")   

$("#table1 tbody tr:even").addClass("even")   

3.隐藏一行

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

4.隐藏一列 

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

方法二:

[html]   view plain copy

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

5.删除一行

//删除除第一行外的所有行   

$("#table1 tr:not(:first)").remove();  

//删除指定行  

$("#table1 tr:eq(3)").remove();  

6.删除一列

//删除除第一列外的所有列   

$("#table1 tr th:not(:nth-child(1))").remove();  

$("#table1 tr td:not(:nth-child(1))").remove();  

//删除第一列  

$("#table1 tr td::nth-child(1)").remove();  

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

//设置table1,第2个tr的第一个td的值。   

$("#table1 tr:eq(1) td:nth-child(1)").html("value");   

//获取table1,第2个tr的第一个td的值。   

$("#table1 tr:eq(1) td:nth-child(1)").html();  

8.插入一行:

< strong > //在第二个tr后插入一行   

$(" < tr > < td > 插入3 </ td > < td > 插入 </ td > < td > 插入 </ td > < td > 插入 </ td> </ tr > ").insertAfter($("#table7 tr:eq(1)")); </ strong >   

9、获取每一行指定的单元格的值

var  arr  = [];  

$("#table1 tr td:nth-child(1)").each(function (key, value) {  

arr.push($(this).html());  

});  

var  result  =  arr .join(',');  

10、全选或全不选

//方法一:  

//全选或全不选 此传入的参数为event 如:checkAll(event)  

 function checkAll(evt)  

 {  

  evt = evt ?evt:window.event;  

 var  chall = evt .target?evt.target:evt.srcElement;  

 var  tbl =$("#table1");  

 var  trlist = tbl .find("tr");  

 for(var  i = 1 ;i < trlist.length ;i++)  

 {  

 var  tr =$(trlist[i]);  

 var  input = tr .find("INPUT[ type = 'checkbox' ]");  

 input.attr("checked",chall.checked);  

 }  
 }  
  
//方法二:  
 

//全选或全不选 此传入的参数为this 如:checkAll(this)  

function checkAll(evt)  

{  

var  tbl =$("#table1");  

var  trlist = tbl .find("tr");  

for(var  i = 1 ;i < trlist.length ;i++)  

{  

var  tr =$(trlist[i]);  

var  input = tr .find("INPUT[ type = 'checkbox' ]");  

input.attr("checked",evt.checked);  

}  
}  
  
//方法三:  

//全选或全不选 此传入的参数为this 如:checkAll(this)  

function checkAll(evt)  

{  

$("#table1 tr").find("input[ type = 'checkbox' ]").each(function(i){  

$(this).attr("checked",evt.checked)  

});  
}  
  
//方法四:  

//全选或全不选 此传入的参数为this 如:checkAll(this)  

function checkAll(evt)  

{  

$("#table1 tr").find("input[ type = 'checkbox' ]").attr("checked",evt.checked);  

}  

11、客户端动态添加行、删除行

function btnAddRow()  

{  

//行号是从0开始,最后一行是新增、删除、保存按钮行 故减去2  

var  rownum =$("#table1 tr").length-2;  

var  chk = "<input type='checkbox' id='chk_" +rownum+"'  name = 'chk_"+rownum+"' /> ";  

var  text = "<input type='text' id='txt_" +rownum+"'  name = 'txt_"+rownum+"'   width= '75px' /> ";  

var  sel = "<select id='sel_" +rownum+"' > < option   value = '1' > 男 </ option > < option   value = '0' > 女 </ option > </ select > ";  

var  row = "<tr><td>" +chk+" </ td > < td > "+text+" </ td > < td > "+sel+" </ td > < td> "+text+" </ td > < td > "+text+" </ td > </ tr > ";  

$(row).insertAfter($("#table1 tr:eq("+rownum+")"));   

}  
  

//客户端删除一行  

//每次只能删除一行,删除多行时出错  

function btnDeleteRow()  

{  

$("#table1 tr").find("input[ type = 'checkbox' ]").each(function(i){  

if($(this).attr("checked"))  

{   

if(i!=0)//不能删除行标题  

{  

$("#table1 tr:eq("+i+")").remove();  

}  
}  
});  
}  
  

//这个比上面的要好,可以一下删除多个记录  

function btnDeleteRow()  

{  

$("#table1 tr").each(function(i){  

var  chk =$(this).find("input[ type = 'checkbox' ]");  

if(chk.attr("id")!="checkall")//不能删除标题行  

{  

if(chk.attr("checked"))  

{  

$(this).remove();  

}  
}  
});  
}  
//客户端保存  

function btnSaveClick()  

{  

//find()方法中我暂时不知道如何设定多个筛选条件,所以下面得不到select列表的值  

//$("#table1 tr td").find("input[ type = 'text' ]" || "select").each(function(i){  

//alert($(this).val());  

//});  
  

$("#table1 tr").find("td").each(function(i){  

if($(this).find("input[ type = 'text' ]").length > 0)  

{  

alert($(this).find("input[ type = 'text' ]").val());  

}  

else if($(this).find("select").length > 0)  

{  

alert($(this).find("select").val());  

}  
});  
}  
 
 

jquery遍历table的tr获取td的值实现方法

 

下面小编就为大家带来一篇jquery遍历table的tr获取td的值实现方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
 

html代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
< 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
2
3
4
5
6
7
8
9
10
11
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);
}

方法二:

?
1
2
3
4
5
6
7
8
9
10
11
12
$( "#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);
 
 
});

以上这篇jQuery遍历table的tr获取td的值实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

 

// 举个简单例子

$( "#tableId tr td:last" ).each( function () {  //遍历表格每行最后一列的值
         var  val = Number($( this ).text());  // 当前列的值,转成数字类型
         var  temp = 0;
         if (temp < val ) {  // 比较大小
             temp = val  // 存储大的值
         }
});

转载于:https://www.cnblogs.com/sekai/p/6093656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值