1

我找到了几种在 div 为空但与 mysql 检索值无关时隐藏 div 的解决方案。

<button class="platform_category" id="platform_', $platform['platform_id'],'_category"><a href="', $platform['platform_url'],'" target="_tab">', $platform['platform_category1'],'</a></button>

当 mysql-row 中没有数据时,Jquery 不会隐藏 div,因为它认为 $platform['platform_category1'] 是一个值,即使这只是检索 mysql 值的 php 代码,以防万一。

对于我正在使用的隐藏 jquery 代码:(如果我将 == 替换为 != 它会隐藏)

    $(function() {
    $('.platform_category').each(function() {
        if ($(this).html() == "") {
             $(this).hide();
       }
    }); 
});
4

4 回答 4

2

具有类的按钮具有platform_category作为<a>内部 html 的标记,因此html()方法在这里没有任何意义(它总是会返回<a>)。请尝试text()

$('.platform_category').each(function() {
    if ($(this).text() == "") {
        $(this).hide();
    }
});

好注释(来自@Blazemonger):最好使用$.trim()以下方法进行文本修剪:

if ($.trim($(this).text()) == "") {
    $(this).hide();
}
于 2012-06-12T14:58:52.030 回答
1

我会使用过滤器来检查.platform_category. 见下文,

$(function() {
   $('.platform_category').filter(function() {
       return $.trim($(this).text()) == '';
    }).hide();
});
于 2012-06-12T15:01:58.410 回答
0

问题是 $('.platform_category') 实际上不是空的,因为 div 中总是有一个锚标记。

您可以通过 PHP(最好的)或 JavaScript 两种方式来做到这一点

通过 PHP

   if ($platform['platform_category1'] != "") {
     <button class="platform_category" id="platform_', $platform['platform_id'],'_category">
        <a href="', $platform['platform_url'],'" target="_tab">',$platform['platform_category1'],'</a>   
      </button>

如果你必须通过 JavaScript 来做

    $(function() {
    $('.platform_category').each(function() {
        if ($(this).find('a').html() == "") {
             $(this).hide();
       }
    }); 
});
于 2012-06-12T15:01:35.397 回答
-1
$('.platform_category').each(function() {
    if ($.trim( $(this).text() )  ==  "") {
        $(this).hide();
    }
});
于 2012-06-12T14:55:25.687 回答