使用jQuery操作HTML的table表格的實(shí)例解析
更新時間:2016年03月13日 11:47:56 作者:qiaolevip
這篇文章主要介紹了使用jQuery操作HTML的table表格的實(shí)例解析,包括用jQuery獲取表格總行數(shù)的小技巧,需要的朋友可以參考下
最終效果圖:

簡單來看一下其中的主要部分:
HTML:
.td_back_color {
background-color: red;
}
.td_center {
text-align: center;
font-weight:bold;
}
CSS:
<table id="tbl_count">
<caption>2013春節(jié)聚會收支統(tǒng)計(jì)</caption>
<tr>
<th>姓名</th>
<th>吃飯</th>
<th>KTV</th>
<th>燒烤</th>
<th>支出</th>
<th>應(yīng)付</th>
</tr>
<tr>
<th>周競成</th>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
......
</table>
JavaScript:
var data = [ {
picture : "http://ww1.sinaimg.cn/mw690/51baa38egw1dzl6l2hjchj.jpg",
name : "周競成",
leftTime : "二月五號",
phone : "186****2296",
qq : "",
workedIn : "浙江 杭州",
cost : 200,
project : "吃飯,KTV,燒烤"
}, ........
// 初始化shuju
var init = function() {
var tbl_count = $("#tbl_count tr:gt(0)");
// 吃飯
var meal_text = $("#tbl_count tr:first th:eq(1)").text();
// KTV
var ktv_text = $("#tbl_count tr:first th:eq(2)").text();
// 燒烤
var bbq_text = $("#tbl_count tr:first th:eq(3)").text();
$.each(tbl_count, function(i,v){
var tr_info = tbl_count.eq(i);
for (var i = 0; i < data.length; i++) {
var data_info = data[i];
// 判斷姓名相同的
if(data_info.name == tr_info.find("th:first").text()){
if(data_info.project.indexOf(meal_text) != -1){
tr_info.find("td:eq(0)").addClass("td_back_color");
}
if(data_info.project.indexOf(ktv_text) != -1){
tr_info.find("td:eq(1)").addClass("td_back_color");
}
if(data_info.project.indexOf(bbq_text) != -1){
tr_info.find("td:eq(2)").addClass("td_back_color");
}
tr_info.find("td:eq(3)").addClass("td_center").text("¥"+data_info.cost);
}
}
});
};
PS:jQuery獲取表格總行數(shù)匯總
var rowCount = $('#myTable tr').length;
var rowCount = $('#myTable >tbody >tr').length;
$("#myTable").attr('rows').length;
var rowCount = $('table#myTable:last').index() + 1;
//Helper function that gets a count of all the rows <TR> in a table body <TBODY>
$.fn.rowCount = function() {
return $('tr', $(this).find('tbody')).length;
};
// USAGE:
var rowCount = $('#productTypesTable').rowCount();
alert(jQuery("#jtkList").find("table").eq(0).find("tr").length);
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)獲取table表格第一列值的方法
- JQuery Ajax動態(tài)生成Table表格
- jQuery對table表格進(jìn)行增刪改查
- 基于JQuery的動態(tài)刪除Table表格的行和列的代碼
- JQuery 動態(tài)生成Table表格實(shí)例代碼
- jQuery實(shí)現(xiàn)Table表格隔行變色及高亮顯示當(dāng)前選擇行效果示例
- jQuery實(shí)現(xiàn)table表格checkbox全選的方法分析
- jQuery+CSS實(shí)現(xiàn)的table表格行列轉(zhuǎn)置功能示例
- jQuery實(shí)現(xiàn)table表格信息的展開和縮小功能示例
- jQuery動態(tài)操作表單示例【基于table表格】
- jQuery實(shí)現(xiàn)為table表格動態(tài)添加或刪除tr功能示例
相關(guān)文章
【經(jīng)典源碼收藏】jQuery實(shí)用代碼片段(篩選,搜索,樣式,清除默認(rèn)值,多選等)
這篇文章主要介紹了jQuery實(shí)用代碼片段,包括篩選,搜索,樣式,清除默認(rèn)值,多選等多種功能.具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
jQuery之網(wǎng)頁換膚實(shí)現(xiàn)代碼
用jQuery做網(wǎng)頁換膚確實(shí)是很一個很巧妙,很好的選擇,這是本人在學(xué)習(xí)jQuery中學(xué)的知識,感覺很有用,寫了下來,希望大家有更好的方法或者代碼不足的地方請諒解,本人也是初學(xué)者啊,希望大家互相勉勵互相學(xué)習(xí)。2011-04-04
基于Jquery和html5實(shí)現(xiàn)炫酷的3D焦點(diǎn)圖動畫
這篇文章主要介紹了基于Jquery和html5實(shí)現(xiàn)炫酷的3D焦點(diǎn)圖動畫的相關(guān)資料,焦點(diǎn)圖中的圖片利用了CSS3的相關(guān)特性實(shí)現(xiàn)圖片傾斜效果,從而讓圖片出現(xiàn)3D的視覺效果,感興趣的小伙伴們可以參考一下2016-03-03
基于jquery的loading效果實(shí)現(xiàn)代碼
基于jquery的loading效果實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-11-11
jQuery+CSS3實(shí)現(xiàn)四種應(yīng)用廣泛的導(dǎo)航條制作實(shí)例詳解
這篇文章主要介紹了jQuery+CSS3實(shí)現(xiàn)多種類型的導(dǎo)航條制作實(shí)例詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09

