[英]Hiding a Div using php
I am currently hiding a div based on an if statement.我目前正在根据 if 语句隐藏一个 div。 The method I use is, use
echo
out a css
style of display: none
我使用的方法是,使用
echo
out a css
style of 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?我的问题是,这是隐藏 div 的好方法吗? Is it possible that browser cache the style and therefore ignore the
echo
-ed out css
style?浏览器是否有可能缓存样式并因此忽略
echo
-ed out css
样式?
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>
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"' : '' ); ?> >
That would not be the best way to hide a div.这不是隐藏 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.
由于 PHP 是在服务器端解析的,您不妨让 if 语句包含或排除 div,而不是回显 CSS 类。 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.
对 CSS 类有用的唯一时间是,如果您打算稍后使用 JavaScript 在页面上显示 div,而用户则在页面本身上。
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:这是以前的线程,可以帮助您:
Javascript if else statement to hide and show div 用于隐藏和显示 div 的 Javascript if else 语句
Yes, that is the standard way of hiding a div.是的,这是隐藏 div 的标准方法。 With regard to the browser cache, it shouldn't cache this as it isn't in an external stylesheet.
关于浏览器缓存,它不应该缓存它,因为它不在外部样式表中。
I generally try to avoid using PHP Conditionals inside of CSS;我通常尽量避免在 CSS 中使用 PHP 条件语句; especially inline CSS (CSS that is on the same page).
尤其是内联 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 (eg carousels, sliders, etc.)很多时候需要输出 div 以便它可以使用 JavaScript 重新显示(例如轮播、滑块等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.