javascript - 如果 $count 变量的值为零则隐藏列表元素

标签 javascript php jquery html css

我想隐藏类别元素<li>如果产品$count值为零。 这是我正在处理的页面:http://www.hotsales.com.br/procurar/

“类别”是类别过滤器。如果类别行的产品为零,则必须将其隐藏(可能是 style="display: none;" )。当选择一些 Store (Loja) 时,许多类别将被列为“Computers (0)”,而这些类别不应显示,因为该商店可能是关于鞋子的。

我试过将它放在 foreach 循环的末尾 if ($count = 0) {document.getElementByTagName("li").style.display = "none"}但不起作用。这是显示过滤器的实际完整功能。 <li>元素是在定义 $count 后的 4 行回显生成的:

/*
List categories on the search sidebar
*/
if( !function_exists( 'list_search_sidebar_cats' ) ){
function list_search_sidebar_cats( $ancestors, $parent, $selected = '', $search_show_count, $permalink ){
global $slugs;
$children = get_terms( 'offer_cat', array( 'parent' => $parent ) );
if( !empty( $children ) ){
    echo '<ul class="list-unstyled">';
    foreach( $children as $child ){
        $li_class = in_array( $child->term_id, $ancestors ) ? 'active' : '';
        if( !empty( $selected ) ){
            $li_class .= $child->slug == $selected->slug ? ' current' : '';
        }

        $count = '';
        if( $search_show_count == 'yes' ){
            $count = custom_term_count( $child, 'offer_cat' );
        }



        if( empty( $ancestors ) || ( !empty( $ancestors ) && in_array( $child->term_id, $ancestors ) ) || ( !empty( $ancestors ) && !empty( $selected ) && $child->parent == $selected->term_id ) ){
            echo '<li style="display:flex;" class="'.esc_attr( $li_class ).'"><a href="javascript:;" data-cat="'.esc_attr( $child->slug ).'">'.$child->name.' <span class="count">('.$count.')</span></a>';
            if( !empty( $li_class ) ){
                list_search_sidebar_cats( $ancestors, $child->term_id, $selected, $search_show_count, $permalink );
                echo '</li>';
     }
            else{
                echo '</li>';
            }
     }

    }
    echo '</ul>';
}

}

最佳答案

渲染后无需将 javascript 放入该播放中即可对其进行更改。
您可以轻松地将其包含在初始输出中:

// please review your conditions here, they don't make too much sense.
//      if this is true    then this is false   (and vice versa)
if( empty( $ancestors ) || ( !empty( $ancestors ) && in_array( $child->term_id, $ancestors ) ) || ( !empty( $ancestors ) && !empty( $selected ) && $child->parent == $selected->term_id ) ){
       // here's the fix:
       $display = $count>0 ? "flex" : "none";
       echo '<li style="display:'.$display.';" class="'.esc_attr( $li_class ).'"><a href="javascript:;" data-cat="'.esc_attr( $child->slug ).'">'.$child->name.' <span class="count">('.$count.')</span></a>';
       if( !empty( $li_class ) ){
            list_search_sidebar_cats( $ancestors, $child->term_id, $selected, $search_show_count, $permalink );
            echo '</li>';
       } else{
            echo '</li>';
       }
}

关于javascript - 如果 $count 变量的值为零则隐藏列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43009652/

相关文章:

javascript - Google Apps 脚本中的去抖动或限制事件处理程序

php - 为什么 timezone_name_from_abbr 返回空字符串?

javascript - 为什么我无法在 jQuery 中初始化变量?

javascript - 当系列中不存在日期时,Highstock 比较先前的数据点

javascript - 如何通过滑动范围 slider 来改变图像?

javascript - C3 带百分比的图表栏

php - 使用 PHP 在 HTML 表格中显示 MySQL 结果

javascript - 在后续 PHP 代码中使用 Ajax 分配的变量

javascript - 为什么 AngularJS 在使用 ng-bind-html 时会去掉 data- 属性?

javascript - 检查所有数组是否有值且不为空