使用jQuery操作HTML的table表格的實(shí)例解析
來源:易賢網(wǎng) 閱讀:988 次 日期:2016-07-20 15:41:10
溫馨提示:易賢網(wǎng)小編為您整理了“使用jQuery操作HTML的table表格的實(shí)例解析”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了使用jQuery操作HTML的table表格的實(shí)例解析,包括用jQuery獲取表格總行數(shù)的小技巧,需要的朋友可以參考下

最終效果圖:

名單

簡(jiǎn)單來看一下其中的主要部分:

HTML:

.td_back_color { 

  background-color: red; 

.td_center { 

  text-align: center; 

  font-weight:bold; 

CSS:

<table id="tbl_count"> 

      <caption>2013春節(jié)聚會(huì)收支統(tǒng)計(jì)</caption> 

      <tr> 

        <th>姓名</th> 

        <th>吃飯</th> 

        <th>KTV</th> 

        <th>燒烤</th> 

        <th>支出</th> 

        <th>應(yīng)付</th> 

      </tr> 

      <tr> 

        <th>周競(jìng)成</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 : "周競(jìng)成", 

      leftTime : "二月五號(hào)", 

      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); 

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:使用jQuery操作HTML的table表格的實(shí)例解析
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)