gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:25:47 25 4
gpt4 key购买 nike

我想隐藏类别元素<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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com