简体   繁体   English

PHP页面没有使用visibility:none或display:none隐藏DIV

[英]PHP page not hiding a DIV using visibility:none or display:none

A PHP page I am working on can be loaded in several states, in the default state I need one DIV layer to be visible and another one to not be visible - and vice versa. 我正在处理的PHP页面可以加载到几种状态,在默认状态下我需要一个DIV层可见而另一个不可见 - 反之亦然。 The script has javascript backup to do the same things, BUT I need the css to work for persons viewing the page with javascript disabled. 该脚本有javascript备份来做同样的事情,但我需要css为查看页面的人工作,禁用javascript。

The code for the first DIV works fine: 第一个DIV的代码工作正常:

<pre><</pre>div name="MoreDiv" ID="MoreDiv" style="position: absolute;top: 10px; left: 15px;  width: 95%; font-size:1.3em; 
<?php
if(isset($_GET['page']))
{
$ipi = getenv("REMOTE_ADDR");
$u = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");

$page = $_GET['page'];
switch($page)
{
case 404:
{echo '';
break;}

default:
{ ?>

<?php }

break;}
} 
else { //
?>visibility:none; display:none;<?php }
?>">

... So you'd think that for the other DIV, that needs to be hidden when the ?page variable is set in the URL, putting the "visibility:none; display:none;" ...所以你认为对于其他DIV,当在URL中设置?页面变量时需要隐藏它,将“visibility:none; display:none;”放入 attributes inside the default for the php output would work - a bit like this: php输出的默认值内的属性可以工作 - 有点像这样:

div name="PhotoText" ID="PhotoText" style="position: absolute;top: 15px; left: 15px; width: 400px; font-size:1.3em; background: rgb(0, 0, 0); background: rgba(0, 0, 0, 0.7); padding:10px; -webkit-border-radius: 10px;  -moz-border-radius: 10px; border-radius: 10px; -khtml-border-radius: 10px; <?php
if(isset($_GET['page']))
{$page = $_GET['page'];
switch($page)
{
case "test":
{?>
visibility:none; display:none;

<?php
break;}

default:
{ ?>
visibility:none; display:none;

<?php }

break;}
}   

else { //
?><?php }
?>">

And yet for some strange and really bizarre reason the DIV STILL SHOWS!... and it stays visible until the page has fully loaded and the javascript within the "body onload" tag kicks in to hide it. 然而,对于一些奇怪且非常奇怪的原因,DIV仍然显示!...并且它一直保持可见,直到页面完全加载并且“body onload”标签内的javascript开始隐藏它。 I cannot use the PHP to keep the DIV layer blank for the default URL variable, as there are javascript links which require it to be there in order to load content.... so I need a non-JS way of hiding this layer, but in such a way that JS can be used to make it visible again!! 我无法使用PHP将DIV层保留为默认的URL变量,因为有javascript链接要求它在那里以加载内容....所以我需要一种非JS方式来隐藏这个层,但是这样可以让JS再次显示它!

There's really several ways of accomplishing this. 有几种方法可以实现这一目标。 First of all, never , ever , interpolate an if and a switch type of logic within an attribute's value as you have it in your question. 首先, 永远永远 ,内插一个ifswitch 属性的值之内 ,你在你的问题有它的逻辑的类型。 Recipe, meet disaster. 食谱,遇见灾难。

On the visibility vs display question, this QA actually depicts it pretty well: visibilitydisplay问题上,这个QA实际上很好地描述了它:

http://webdesign.about.com/od/css/f/blfaqhidden.htm http://webdesign.about.com/od/css/f/blfaqhidden.htm

More or less, visibility still maintains it's "space" in the flow of the document, whereas display affects it's flow (including removing it entirely). 或多或少, visibility仍然保持它在文档流中的“空间”,而display影响它的流程(包括完全删除它)。 Easy to mix up, totally different concepts. 容易混淆,完全不同的概念。

Here's a demo , and the source markup and CSS. 这是一个演示 ,以及源标记和CSS。

<div id="wrapper">
    <div id="novisibility" class="test">
        <p>Visiblity<br>Will you see me?<br>Will I still be "there"?</p>
    </div>
    <div id="nodisplay" class="test">
        <p>Block<br>Will you see me?<br>Will I disappear?</p>
    </div>
    <p id="buttons">
        <button type="button" id="visibility">Toggle Visibility</button>
        <button type="button" id="display">Toggle Display</button>
    </p>
</div>

#wrapper.hide #nodisplay {
    display: none;
}
#wrapper.invisible #novisibility {
    visibility: hidden;
}

The Javascript is toggling the .hide and .novisibility classes on the #wrapper . JavaScript已经拨动.hide.novisibility的类#wrapper

I would suggest a template pattern, where you separate the logic from the markup/display layer. 我建议使用模板模式,将逻辑与标记/显示层分开。 For instance: 例如:

page.php page.php文件

<?php

// ... other stuff

if (isset($_GET['page'])) {
    $page = $_GET['page'];

    switch($page) {
        case "test":
           $cls = " NoDisplay";
        break;
        default:
           $cls = " Highlight";
        break;
    }
}

// ... other stuff

?>

Where you hae this defined in your CSS (not in a style attribute if you can help it): 您在CSS中定义的内容(如果您可以帮助它,则不在style属性中):

/assets/css/site.css /assets/css/site.css

.PhototextContent.NoDisplay {
    display: none;
}
.PhototextContent.Highlight {
    background: yellow;
    display: block;
}

docPagePhotoInline.tpl docPagePhotoInline.tpl

<!-- Other photo stuff, I presume. -->
<div id="PhotoText" class="PhotoTextContent<?php echo $cls; ?>">Your content</div>

I also wonder if you're reusing that id value; 我也想知道你是否正在重用那个id值; you should (can) only have one unique id per page . 应该(只)每页只有一个唯一的id If you need something to cover several elements, use class names to group elements by class . 如果需要覆盖多个元素,请使用class名按对元素进行分组。 Also note, name in div s are still valid I think, but rarely used by anyone. 另请注意,我认为divname仍然有效,但很少被任何人使用。 Use id to uniquely identify an element. 使用id来唯一标识元素。

So which do you think is easier to read? 那你认为哪个更容易阅读? I don't think it's hard to see what I'm up to here, but it takes a double look with what you've got, followed by some head-scratching. 我不认为我很难看到我在这里做什么,但它需要仔细研究你所得到的东西,接着是一些令人头疼的问题。 Your code should be expressive of intent; 你的代码应该表达意图; code that feels well-tended and that feels right when you look at it isn't always great code, but inelegant, whiplash formatting is often an indicator the code could be problematic. 感觉良好且感觉合适的代码并不总是很好的代码,但不优雅的鞭打格式通常表明代码可能有问题。 Think of it like a garden; 把它想象成一个花园; a well tended and organized garden is much more appealing than one that's, well, not. 一个精心设计和有组织的花园比那个,更好,没有更具吸引力。

So I'd implement at least a basic templating approach. 所以我至少要实现一个基本的模板方法。 There's several decent tutorials out there, like this one . 那里有几个体面的教程,就像这个 I'd like to try out Mustache for PHP soon, but there's others like Smarty , Flexy , Twig , and Dwoo . 我想尽快尝试使用Mustache for PHP ,但还有其他像SmartyFlexyTwigDwoo Keep in mind PHP itself may be used as a templating system, and most template system compile the templates into PHP or bytecode. 请记住,PHP本身可以用作模板系统,大多数模板系统将模板编译成PHP或字节码。 You just need to be careful to separate the logic from the view or display layer. 您只需要小心将逻辑与视图或显示层分开。

You should pair them with an approach, though, so I'd suggest a framework using the MVC pattern . 但是,您应该将它们与方法配对,因此我建议使用MVC模式的框架。 There's a lot out there, like CodeIgniter , TinyMVC , Kohana , Cake , etc. This looks reasonable: 那里有很多,比如CodeIgniterTinyMVCKohanaCake等。这看起来很合理:

http://phpsavant.com/ http://phpsavant.com/

Try not to hang out there on your own too much, though. 尽管如此,尽量不要自己在那里闲逛。

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

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