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;

 


免責聲明!

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



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