javascript - 如何仅获取带有值的 html 表格行列?

标签 javascript jquery

我想知道是否有一种更简单的方法来仅获取带有值的 html 表格行列,

这是一个示例表格图像

enter image description here

这就是我想要的结果

enter image description here

 <table id="tblPayslipApproval" cellspacing="0" border="0">
    <tbody>
    <tr class="tr-border-full">
      <td style="min-width:50px">EARNINGS</td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="td-border-right"></td>
      <td style="min-width:50px" align="center" colspan="3" class="td-border-right">DEDUCTIONS</td>
      <td style="min-width:50px" align="center" colspan="3" class="td-border-right">LOAN BALANCES</td>
    </tr>
    <tr class="tr-border-full">
      <td style="min-width:50px">DESCRIPTION</td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right">AMOUNT</td>
      <td style="min-width:50px">DESCRIPTION</td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right">AMOUNT</td>
      <td style="min-width:50px">DESCRIPTION</td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right">AMOUNT</td>
    </tr>
    <tr>
      <td style="min-width:50px" class="td-border-left">&nbsp;</td>
      <td style="min-width:50px">&nbsp;</td>
      <td style="min-width:50px" class="td-border-right">&nbsp;</td>
      <td style="min-width:50px">&nbsp;</td>
      <td style="min-width:50px">&nbsp;</td>
      <td style="min-width:50px" class="td-border-right">&nbsp;</td>
      <td style="min-width:50px">&nbsp;</td>
      <td style="min-width:50px">&nbsp;</td>
      <td style="min-width:50px" class="td-border-right">&nbsp;</td>
    </tr>
    <tr>
      <td style="min-width:50px" class="td-border-left">Basic Pay</td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"><span id="preview_basic_pay"> 18,655.88 </span></td>
      <td style="min-width:50px">SSS EE Contribution</td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"><span id="preview_sss_ee_contrib1"> 581.30 </span></td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"><span id="preview_sss_ee_contrib2"></span></td>
    </tr>
    <tr>
      <td style="min-width:50px" class="td-border-left"></td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"></td>
      <td style="min-width:50px">PhilHealth EE Contribution</td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"><span id="preview_philhealth_ee_contrib1"> 256.52 </span></td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"></td>
    </tr>
    <tr>
      <td style="min-width:50px" class="td-border-left"> </td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"></td>
      <td style="min-width:50px">PAGIBIG EE Contribution</td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"><span id="preview_pagibig_ee_contrib1"> 100.00 </span></td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px"></td>
      <td style="min-width:50px" class="number-align td-border-right"></td>
    </tr>
    </tbody>
    </table>

这是我的草稿代码,我不确定我是否走在正确的道路上,或者是否有更简单的方法,

$.each("table > tbody > tr:nth-child(3)", function(){
    var $elem = $(this);
    if($elem.text() == " - "){
       $elem.prev().text("");
       $elem.text("");
    }
})

$.each("table > tbody > tr:nth-child(6)", function(){
    var $elem = $(this);
    if($elem.text() == " - "){
       $elem.prev().text("");
       $elem.text("");
    }
})

$.each("table > tbody > tr:nth-child(9)", function(){
    var $elem = $(this);
    if($elem.text() == " - "){
       $elem.prev().text("");
       $elem.text("");
    }
})

删除“-”列的值后,我必须排列行,并填补空白,

有没有其他方法可以让它变得更简单

谢谢!

最佳答案

你可以尝试类似的方法

$(document).ready(function () {

    $("#tblPayslipApproval tr").find("td:contains('-')").each(function(k,v){
        $(v).prev().prev().html("");
        $(v).html("");
    });


});

关于javascript - 如何仅获取带有值的 html 表格行列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49612386/

相关文章:

javascript - 根据用户计算机的性能禁用 JavaScript 功能

javascript - 检查一个必须包含另一个字符串的字符串

javascript - polymer 单向绑定(bind)不起作用

jquery - 在 MVC 中创建动态 Html 控件的最佳方法

javascript - 淡入前瞬间闪烁页面

javascript - jQuery 将 td 附加到动态创建的 tr

javascript - 以匀速圆周运动同时拖动元素

javascript - 如何模拟 GET 操作并将其传递到同一页面而不重新加载?

javascript - 如何用 'v-for' 实现 'v-if' ?

jquery - 解析独立网站? (或者, "Scaling an entire website to fill the browser")