匯總常用的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  // 存儲大的值
         }
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM