使用 php 隐藏 Div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11855535/
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
Hiding a Div using php
提问by Krimson
I am currently hiding a div based on an if statement. The method I use is, use echoout a cssstyle of display: none
我目前正在根据 if 语句隐藏一个 div。我使用的方法是,用echo出一种css风格display: none
Here is what I am doing specifically:
这是我正在做的具体事情:
<style>
#content{
<?php
if(condition){
echo 'display:none';
}
?>
}
</style>
<body>
<div id="content">
Foo bar
</div>
</body>
My question being, Is this a good method for a hiding a div? Is it possible that browser cache the style and therefore ignore the echo-ed out cssstyle?
我的问题是,这是隐藏 div 的好方法吗?浏览器是否有可能缓存样式并因此忽略echo-ed outcss样式?
回答by Doc Roms
Using Php in your CSS (Cascade Style Sheet) is not "proper",
在你的 CSS(层叠样式表)中使用 PHP 是不“正确的”,
Also, You can use Php in your HTML:
此外,您可以在 HTML 中使用 PHP:
<body>
<?php if (condition){ ?>
<div id="content">
Foo bar
</div>
<?php } ?>
</body>
With this code, div block don't appear, (and you don't use it with JavaScript), You can use this for just hidden your div :
使用此代码,不会出现 div 块(并且您不会将它与 JavaScript 一起使用),您可以使用它来隐藏您的 div :
<body>
<div id="content" <?php if (condition){ echo 'style="display:none;"'; } ?>>
Foo bar
</div>
</body>
回答by insertusernamehere
Why not create a class:
为什么不创建一个类:
<style>
.hidden {
display: none;
}
</style>
And than apply it with PHP:
然后用 PHP 应用它:
<div id="content" <?php print ( condition ? 'class="hidden"' : '' ); ?> >
回答by Kyle Ross
That would not be the best way to hide a div. Since PHP is parsed server-side, you might as well have the if statement include or exclude the div instead of echoing a CSS class. The only time that would be useful with a CSS class is if you plan to use JavaScript to show the div on the page later on, while the user is on the page itself.
这不是隐藏 div 的最佳方式。由于 PHP 是在服务器端解析的,您不妨让 if 语句包含或排除 div,而不是回显 CSS 类。对 CSS 类有用的唯一时间是,如果您打算稍后使用 JavaScript 在页面上显示 div,而用户则在页面本身上。
回答by LOZ
You could:
你可以:
<div runat="server" id="theDiv">
Code behind is
{
theDiv.Visible = False;
}
or
或者
Most people would use javascript
大多数人会使用javascript
Heres a previous thread that will help you out:
这是以前的线程,可以帮助您:
回答by John Wheal
Yes, that is the standard way of hiding a div. With regard to the browser cache, it shouldn't cache this as it isn't in an external stylesheet.
是的,这是隐藏 div 的标准方法。关于浏览器缓存,它不应该缓存它,因为它不在外部样式表中。
回答by Jarrett Barnett
I generally try to avoid using PHP Conditionals inside of CSS; especially inline CSS (CSS that is on the same page).
我通常尽量避免在 CSS 中使用 PHP 条件语句;尤其是内联 CSS(在同一页面上的 CSS)。
I would keep your CSS in its own CSS file and use the PHP conditional to either add a "hide" class to the DIV -OR- not echo the DIV at all.
我会将您的 CSS 保留在自己的 CSS 文件中,并使用 PHP 条件将“隐藏”类添加到 DIV - 或者 - 根本不回显 DIV。
<link rel="stylesheet" type="text/css" href="style.css" />
<body>
<div id="content" <?php if(conditional) : ?>class="hide"<?php endif;?>>
Foo bar
</div>
</body>
or alternatively
或者
<?php $class = (conditional) ? "hide" : ""; ?>
<link rel="stylesheet" type="text/css" href="style.css" />
<body>
<div id="content" class="<?=$class?>">
Foo bar
</div>
</body>
or
或者
<link rel="stylesheet" type="text/css" href="style.css" />
<body>
<?php if (conditional) : ?>
<div id="content">
Foo bar
</div>
<?php endif; ?>
</body>
Many times the div needs to be outputted so it can be re-displayed using JavaScript (e.g. carousels, sliders, etc.)
很多时候需要输出 div 以便它可以使用 JavaScript 重新显示(例如轮播、滑块等)

