雖然現在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();
//★★★ 補充 ★★★
1.渲染列表時,需要設置前3個元素的margin-top值與其他的不同
:nth-child(-n+3){
margin-top: 12px;
}
2.選擇前幾個元素
/*【負方向范圍】選擇第1個到第6個 */
:nth-child(-n+6){}
3.從第幾個開始選擇
/*【正方向范圍】選擇從第6個開始的 */
:nth-child(n+6){}
4.兩者結合使用,可以限制選擇某一個范圍
/*【限制范圍】選擇第6個到第9個,取兩者的交集【感謝小伙伴的糾正~】 */
:nth-child(-n+9):nth-child(n+6){}
//★★ end ★★★
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);
}
客戶端刪除一行
//每次只能刪除一行,刪除多行時出錯
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());
}});
}
來源:
https://www.cnblogs.com/oktell/p/5100957.html
https://blog.csdn.net/weixin_39015132/article/details/82015012