用Jquery選擇器計算table中的某一列某一行的合計
來源:易賢網(wǎng) 閱讀:2259 次 日期:2014-08-15 13:59:02
溫馨提示:易賢網(wǎng)小編為您整理了“用Jquery選擇器計算table中的某一列某一行的合計”,方便廣大網(wǎng)友查閱!

利用Jquery選擇器,計算table中的某一列,某一行的合計,非常方便。下面以計算行合計為例:

核心算法:

view sourceprint?1 $('#tableId tr').each(function() {

2 $(this).find('td:eq(columnIndex)').each(function() {

3 totalAmount += parseFloat($(this).text());

4 })

5 });

下面是案例代碼

view sourceprint?01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

02 <html xmlns="http://www.w3.org/1999/xhtml">

03 <head>

04 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

05 <title>Jquery計算table行合計</title>

06 <script id="jquery_183" type="text/javascript" class="library" src="http://runjs.cn/js/sandbox/jquery/jquery-1.8.3.min.js"></script>

07 <script type="text/javascript">

08 $(document).ready(function() {

09

10 var totalRow = 0

11 $('#mytable tr').each(function() {

12 $(this).find('td:eq(2)').each(function(){

13 totalRow += parseFloat($(this).text());

14 });

15 });

16

17 $('#totalRow').append('<td>合計</td><td></td><td>'+totalRow+'</td><td></td>');

18 });

19 </script>

20

21 </head>

22 <body style="width:100%; height:100%;">

23 <table id="mytable" border="1" width="37%">

24 <thead>

25 </thead>

26 <tr>

27 <td width="63" >11</td>

28 <td width="68" >12</td>

29 <td width="62" >13</td>

30 <td width="75" >14</td>

31 </tr>

32 <tr>

33 <td width="63" >21</td>

34 <td width="68" >22</td>

35 <td width="62" >23</td>

36 <td width="75" >24</td>

37 </tr>

38 <tr id="totalRow"></tr>

39 </table>

40 </body>

41 </html>

效果圖:

名單

更多信息請查看IT技術專欄

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:用Jquery選擇器計算table中的某一列某一行的合計

2025國考·省考課程試聽報名

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