【问题标题】:jQuery Hide block if td empty如果 td 为空,则 jQuery 隐藏块
【发布时间】:2014-01-21 12:19:24
【问题描述】:

如果 td 为空(没有adiv's),我需要隐藏一个表。

对不起,我的错

如果有内容,就没有什么可隐藏的不需要 但如果 td 为空 - 需要隐藏表格

<table class="klist-actions">
    <tr>
        <td class="klist-actions-goto">
            <a name="forumbottom"></a>
        </td>
        <td class="klist-actions-forum">
            <div class="kmessage-buttons-row">
                <a class="kicon-button kbuttoncomm btn-left" href="#"></a>                      
            </div>
        </td>

        <td class="klist-pages-all">
        </td>
    </tr>
</table>

【问题讨论】:

标签: jquery hide


【解决方案1】:

如果任何td 元素为空,此代码将隐藏表格:

//select all td elements, iterate through them and check the length of their content
$(".klist-actions td").each(function(i,e){
    if(!$.trim(e.innerHTML).length){
       $(e).parents("table").hide();
    }
});

JS 小提琴: http://jsfiddle.net/XJd8t/7/

【讨论】:

  • 如果表格包含空 td,我会隐藏。
  • $(".klist-actions td").hide(); 但是,这将隐藏您未指定的所有 TD 1。您需要在“e”上进行隐藏
  • @NetaMeta 根据 OP I need to hide a table。要求有点模糊,这就是为什么我提到它会在描述中隐藏整个表格。
  • @KevinBowersox 是的,这就是我所缺少的 I need to hide a table if td is empty
  • 然后`e.parents('table').hide();还是我错过了什么?
【解决方案2】:

如果要隐藏具有空 td 元素的表格,可以使用 .filter() 方法:

$('table').filter(function(){
    return $('td', this).filter(function() {
       return $.trim(this.innerHTML).length === 0;
    }).length;
}).hide();

如果你想隐藏所有td后代为空的表格,你可以比较td后代的长度和空的长度,如果它们都是空的,隐藏表格:

$('table').filter(function() {
    var $tds = $('td', this);
    return $tds.filter(function() {
       return $.trim(this.innerHTML).length === 0;
    }).length === $tds.length;
}).hide();

【讨论】:

    【解决方案3】:

    下面的代码将隐藏任何包含至少一个“td”但没有“a”或“div”的“表”

    $('table:has(td:not(:has(a div)))').hide();
    

    jQuery 有很多有趣的选择器,阅读它们here

    【讨论】:

      【解决方案4】:

      谢谢朋友!此代码有效!!!

      $('table').filter(function() {
          var $tds = $('td', this);
          return $tds.filter(function() {
             return $.trim(this.innerHTML).length === 0;
          }).length === $tds.length;
      }).hide();
      

      【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2011-11-27
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2012-01-08
      相关资源
      最近更新 更多