【问题标题】:Hiding table rows if any of the two columns is empty如果两列中的任何一列为空,则隐藏表格行
【发布时间】:2011-09-07 08:28:44
【问题描述】:

有一个 2 列的表,其中每列由另一个包含一些数据的表组成,如果两列都是空的,我想要做的是隐藏整行.. 任何人都可以在上面分享一些输入..

http://jsfiddle.net/m38uL/

提前致谢

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案1】:

    您已经接近工作,但您的 if 声明中有错误:

    if ($(this).find('.firstTab').text() == '' && (this).find('.secondTab').text() == '')
    

    请注意第二个条件中缺少的$。如果.firstTab.secondTab 元素中有空格,它也会失败。就个人而言,我会稍微不同地编写代码,使用filter 方法和.trim 删除内容开头或结尾的任何空白:

    $(".parentRow > td").filter(function() {
           return $.trim($(this).find(".firstTab").text) == "" && $.trim($(this).find(".secondTab").text()) == "";
    }).remove();
    

    这是上述代码的live example

    【讨论】:

    • 没问题,很高兴我能帮上忙 :) 不要忘记接受对你有帮助的答案!
    • 该代码有一个小问题,如果该列为空,则该列将被删除,只有当该行中的两个表都为空时才会发生这种情况..您能建议如何更改它
    【解决方案2】:
    $(function() {
      $('.parentRow').each(function() {
        if ($(".firstTab", this).text() === '' && $('.secondTab', this).text() === '')
        {
          $(this).hide();
        }
      });
    });
    

    【讨论】:

      【解决方案3】:

      这样的事情怎么样:

      // loop through each row...
      $('.parentRow').each(function() {      
        var rowEmpty = true;
      
         // check each cell in the row to see if it's got anything in it
         $($this).find('td').each(function(){
             if($(this).html() != "")
             {
                 rowEmpty = false;
             }
         });
      
         // if the row was empty, hide it
         if(rowEmpty)
         {
          $(this).hide();   
         }
      }); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-17
        • 1970-01-01
        • 2012-02-18
        • 2022-01-16
        • 2022-10-15
        相关资源
        最近更新 更多