2014-09-12 108 views
1

似乎有很多关于隐藏空div的回答问题,但是在我尝试了几次后没有运行它,我问自己题。我在这个页面上有这个部分来显示各种东西,在这种情况下它是标题。如果一个包含php的div标签是空的,隐藏div或者显示某些文本

这是它的外观时,有一个标题显示:

enter image description here

这是它的外观时,有没有标题显示:

enter image description here

这就是我想让它看起来像没有标题显示的样子:

enter image description here

下面是代码部分,与该领域涉及的问题的关注,标题被后一名成员多少分都和functions.php中收集收集,所以我想的东西,可以阅读,没有标题从functions.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> 
+0

只要在最后显示“No Title”即可。 '<?如果($ gender ==='Mare'){echo find_asda_title($ horse);} elseif($ gender ==='Stallion'){echo find_asda_title($ horse);} elseif($ gender ==='Gelding' ){echo find_asda_title($ horse);} else {echo'No Title';}?>' – APAD1 2014-09-12 21:44:00

+0

如果我这样做,它会返回6没有标题,我只想它返回一个,只有当在实际上的PHP返回完全空 – user3481279 2014-09-12 21:52:43

+0

使用@ MarcB的建议 – APAD1 2014-09-12 21:53:08

回答

2

大部分代码似乎完全没有意义/重复。为什么不更多的东西一样:

只是针对与:empty选择的空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 
} 
+0

这不幸的是,我不希望它的工作,因为它会更干净,重复性更低。但是我尝试了它,并且按照与开始时相同的顺序输入了所有这些内容,唯一发生的事情是该页面停止工作。 – user3481279 2014-09-12 21:54:07

0

你可以,如果你想通过CSS处理空白的div元素的含量

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

如果我删除:空白部分文本无标题显示,否则没有任何反应。但没有:它总是显示空的。任何想法为什么它不响应︰空,是因为该div实际上包含几个PHP行已读? – user3481279 2014-09-12 22:02:57

+0

不,因为PHP在服务器上得到处理,它不会以呈现的HTML显示。 ':empty'选择器不工作的唯一方法是如果你的div实际上不是空的! – LcSalazar 2014-09-12 22:05:41

+0

@ user3481279,看看在这里工作的效果:http://jsfiddle.net/usytdv9r/ – LcSalazar 2014-09-12 22:05:59