繁体   English   中英

如果包含php的div标签为空,则隐藏div或显示某些文本

[英]If a div tag that includes php is empty, hide the div or display certain text

关于隐藏空div似乎有很多已回答的问题,但是在我尝试了几个没有成功的div后,我问了自己一个问题。 我的页面上有此部分,以显示各种情况(在本例中为标题)。

这是显示标题时的外观:

在此处输入图片说明

没有标题显示时的外观如下:

在此处输入图片说明

这是我希望没有标题显示时的样子:

在此处输入图片说明

以下是处理此问题所涉及领域的代码部分,标题是在成员拥有多少点之后收集的,并且是从functions.php中收集的,所以我想要一些可以读取的内容,即没有标题可以从函数中收集.php,然后显示难看的小方块,而不显示丑陋的标题,如果有人对此有答案,我会很高兴,我已经使用了几个小时,但没有成功,我应该说我是一个新手当涉及到php时,甚至当涉及到javascript和jquery时也是如此。

 <div id='dumb'>
    <div id="assocpoint2">TITEL/S:</div><div id="assocpointprint2">
    <? if($gender === 'Mare'){echo find_asda_title($horse);}elseif($gender === 'Stallion'){echo find_asda_title($horse);}elseif($gender === 'Gelding'){echo find_asda_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_asea_title($horse);}elseif($gender === 'Stallion'){echo find_asea_title($horse);}elseif($gender === 'Gelding'){echo find_asea_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_asja_title($horse);}elseif($gender === 'Stallion'){echo find_asja_title($horse);}elseif($gender === 'Gelding'){echo find_asja_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_sisf_title($horse);}elseif($gender === 'Stallion'){echo find_sisf_title($horse);}elseif($gender === 'Gelding'){echo find_sisf_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_aspa_title($horse);}elseif($gender === 'Stallion'){echo find_aspa_title($horse);}elseif($gender === 'Gelding'){echo find_aspa_title($horse);} ?>
    <? if($gender === 'Mare'){echo find_aswa_title($horse);}elseif($gender === 'Stallion'){echo find_aswa_title($horse);}elseif($gender === 'Gelding'){echo find_aswa_title($horse);} ?>
    </div></div>

大部分代码似乎毫无意义/重复。 为什么不这样:

$output = find_asda_title($horse) . find_asea_title($horse) . etc...
if (in_array($gender, array('Mare', 'Gelding', 'Stallion')) {
   echo $output;
} else {
   echo stuff to hide the div
}

如果需要,可以通过CSS处理空div的内容:

只需使用:empty选择器将空div作为目标,然后向其添加伪内容即可:

#assocpointprint2:empty:after {
   content: "No Title";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM