jQuery - check for empty TDs and if all empty hide the parent TR

前端 未结 5 2517
眼角桃花
眼角桃花 2021-02-20 01:57

I\'d like to check for empty TDs (classes a-h only) and if all are empty, then I\'d like to hide the parent TR. The issues I\'m running into are, my empty TDs contain \" \"

5条回答
  •  情歌与酒
    2021-02-20 02:53

    "If Empty" is not clear, since in your example, they are filled with "nbsp". This is the reason why I made a function called isEmpty() so you can define your custom rules in there. Since you didn't want requirementRight, I put td:not(.requirementRight). This is not the correct way to do it.

    The correct way to do it, would be to put a second class on a-h, such as

    
        Requirement
         
         
         
         
         
        NOT EMPTY
         
         
      
    

    So we can call tr.find(tr.checkempty)..

    Anyway, here's the code!

    $("tr").each(function() {
      var trIsEmpty = true;
      var tr = $(this);
    
        tr.find("td:not(.requirementRight)").each(function() {
          td = $(this);
    
            if (isEmpty(td) === false)  {
             trIsEmpty = false;   
            }
        });
    
        if (trIsEmpty == true) {
             tr.hide();                       
        }
    });
    
        function isEmpty(td) {
            if (td.text == '' || td.text() == ' ' || td.html() == ' ' || td.is(":not(:visible)")) {
                return true;
            }            
    
            return false;
        }
    ​
    

    Working Example: http://jsfiddle.net/FeQ7h/7/

提交回复
热议问题