简体   繁体   English

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

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

There seem to be a ton of answered questions about hiding empty divs, but after I have tried several with no luck of it working I ask my own question. 关于隐藏空div似乎有很多已回答的问题,但是在我尝试了几个没有成功的div后,我问了自己一个问题。 I have this section on my page to display various things in this case it's titles. 我的页面上有此部分,以显示各种情况(在本例中为标题)。

This is how it looks when there is a title to display: 这是显示标题时的外观:

在此处输入图片说明

This is how it looks when there is no title to display: 没有标题显示时的外观如下:

在此处输入图片说明

This is how what I want it to look like when there are no title to display: 这是我希望没有标题显示时的样子:

在此处输入图片说明

Below is the code part that deals with this area the question concerns, the titles are being collected after how many points a member have and are collected from functions.php, so I want something that can read that there are no title to collect from functions.php and then instead of the ugly little square it shows No title if anyone has the answer to this I will be very happy, I have been on it for hours but with no success, I should say to that I am very much a novice when it comes to php and not even that when it comes to javascript and jquery. 以下是处理此问题所涉及领域的代码部分,标题是在成员拥有多少点之后收集的,并且是从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>

Most of that code seems to be utterly pointless/repetitive. 大部分代码似乎毫无意义/重复。 Why not something more like: 为什么不这样:

$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
}

You can handle the content of empty divs through css if you want: 如果需要,可以通过CSS处理空div的内容:

Just target the empty div with the :empty selector, and add a pseudo content to it: 只需使用:empty选择器将空div作为目标,然后向其添加伪内容即可:

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

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

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