php Show Hide div if, if 语句为真
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7020873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show Hide div if, if statement is true
提问by Christian
My code works to a point. What I want is that when this if statement is false, the <div>
doesn't show
我的代码在一定程度上起作用。我想要的是,当这个 if 语句为假时,<div>
不显示
<?php
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
if ($numrows > 0) {
$fvisit = mysql_fetch_array($result3);
}
else {
}
?>
回答by munjal
You can use css or js for hiding a div. In else statement you can write it as:
您可以使用 css 或 js 来隐藏 div。在 else 语句中,您可以将其写为:
else{
?>
<style type="text/css">#divId{
display:none;
}</style>
<?php
}
Or in jQuery
或者在 jQuery 中
else{
?>
<script type="text/javascript">$('#divId').hide()</script>
<?php
}
Or in javascript
或者在javascript中
else{
?>
<script type="text/javascript">document.getElementById('divId').style.display = 'none';</script>
<?php
}
回答by Dreaded semicolon
This does not need jquery, you could set a variable inside the if and use it in html or pass it thru your template system if any
这不需要 jquery,您可以在 if 中设置一个变量并在 html 中使用它或通过您的模板系统传递它(如果有)
<?php
$showDivFlag=false
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
if ($numrows > 0){
$fvisit = mysql_fetch_array($result3);
$showDivFlag=true;
}else {
}
?>
later in html
稍后在 html
<div id="results" <?php if ($showDivFlag===false){?>style="display:none"<?php } ?>>
回答by ShankarSangoli
Use show/hide method as below
使用显示/隐藏方法如下
$("div").show();//To Show
$("div").hide();//To Hide
回答by ddools
A fresh look at this(possibly)
in your php:
在你的 php 中重新审视这个(可能):
else{
$hidemydiv = "hide";
}
And then later in your html code:
然后在你的 html 代码中:
<div class='<?php echo $hidemydiv ?>' > maybe show or hide this</div>
in this way your php remains quite clean
这样你的 php 就很干净了
回答by hyt47
<?php
$divStyle=''; // show div
// add condition
if($variable == '1'){
$divStyle='style="display:none;"'; //hide div
}
print'<div '.$divStyle.'>Div to hide</div>';
?>
回答by Cody Krecicki
Probably the easiest to hide a div and show a div in PHP based on a variables and the operator.
可能是基于变量和运算符在 PHP 中隐藏 div 和显示 div 的最简单方法。
<?php
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
?>
<html>
<?php if($numrows > null){ ?>
no meow :-(
<?php } ?>
<?php if($numrows < null){ ?>
lots of meow
<?php } ?>
</html>
Here is my original code before adding your requirements:
在添加您的要求之前,这是我的原始代码:
<?php
$address = 'meow';
?>
<?php if($address == null){ ?>
no meow :-(
<?php } ?>
<?php if($address != null){ ?>
lots of meow
<?php } ?>
回答by jerryurenaa
from php you can invoke jquery like this but my 2nd method is much cleaner and better for php
从 php 你可以像这样调用 jquery 但我的第二个方法对 php 来说更干净更好
if($switchView) :?>
<script>$('.container').hide();</script>
<script>$('.confirm').show();</script>
<?php endif;
another way is to initiate your class and dynamically invoke the condition like this
另一种方法是启动您的课程并像这样动态调用条件
$registerForm ='block';
then in your html use this
然后在你的 html 中使用这个
<div class="col" style="display: <?= $registerForm?>">
now you can play with the view with if and else easily without having a messed code example
现在您可以轻松地使用 if 和 else 来处理视图,而无需混乱的代码示例
if($condition) registerForm = 'none';
Make sure you use 'block' to show and 'none' to hide. This is far the easiest way with php
确保使用“阻止”来显示和“无”来隐藏。这是使用 php 最简单的方法