jquery合并表头单元格

Ps:本例使用jquery-1.11.3.js

本例的功能为合并<th>,检测字符为下划线

前>>


后>>



 <table>
    <tr><th>cdhe</th><th>cd_he</th><th>cd_s12_1</th><th>cd_s12_2</th><th>ad_he</th><th>cdhe</th><th>cd_he</th><th>cd_s12_1</th><th>cd_s12_2</th><th>ad_he</th></tr>
    
    <tr><td>dcde</td><td>dcde</td><td>dcde</td><td>dcde</td><td>dcde</td><td>dcde</td><td>dcde</td><td>dcde</td><td>dcde</td><td>dcde</td></tr>
    </table>


  $(function () {
         
           $.fn.muliTh = function () {
               var arrs = [], a1 = [], maxLength = 0;
               var that = $(this);
               that.find('th').each(function () {
                   a1 = $(this).html().replace(/^\s|\s$/g, "").replace(/\s+/g, "").split('_');
                   maxLength < a1.length ? maxLength = a1.length : 1;
                   arrs.push(a1);
               })
               that.find('th').hide();
               var createTh = '';
               for (i = 0; i < maxLength; i++) {
                   createTh += "<tr class='jg_func jg_muliTh'>";
                   for (a = 0; b = that.find('th').length, a < b; a++) {
                       if (arrs[a][i]) {
                           createTh += "<th colspan='1'  par='" + (function (list, para) {

                               return para - 1 < 0 ? 'par' : list[para - 1];
                           })(arrs[a], i) + "' rowspan='" + (function (x, y, z) {
                               var rs = 1;
                               !y[z + 1] ? rs = x + 1 - y.length : rs = 1;
                               return rs;
                           })(maxLength, arrs[a], i) + "'>" + arrs[a][i] + "</th>"; //(maxLength + 1 - arrs[a].length) 
                       }

                   }
                   createTh += "</tr>";
               }
               that.prepend(createTh);
               $('.jg_muliTh').each(function () {
                   $(this).find('th').each(function () {

                       var a2 = $(this);
                       if (a2.html() === a2.prev().html() && a2.attr('par') === a2.prev().attr('par')) {
                           a2.prev().attr('colspan', 1 + +a2.prev().attr('colspan'));
                           a2.remove();
                       }
                   })

               })

               console.log(arrs);
           }
       })
       window.onload  = function () {
           $('table').muliTh();
       }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值