1

Is there a "standard" or semantically preferred method of hiding elements? Two easy options are using php:

<?
if ( $_URL == "/page/" ){
?>
<div id="page">
  <div class="hide-or-show">
    stuff
  </div>
</div>
<?
}
<?

or using css:

<css>
.hide .hide-or-show {
  display:none;
}
</css>

<div id="page" class="<? if ( $_URL == "/page/" ) print "hide"; ?>">
  <div class="hide-or-show">
    stuff
  </div>
</div>

Both methods remove the element from the viewer. The php method would send less code. The css method seems cleaner (assuming you're removing more than just one element).

Is one method more "correct" than the other? Or is it just a programmer preference?

4

4 回答 4

1

第一种方法更好。您不会向客户端发送不必要的代码,从而使您的代码性能更好。仅当您需要在某个时刻显示元素时才使用 css 隐藏。

于 2013-03-13T18:43:05.687 回答
0

这里使用 css 是隐藏数据的最佳方式,因为它将在客户端机器上执行,每次使用 php 时总是更可取,我们必须向服务器发出请求,这也是更好的方式,但始终客户端执行是最好和有效的方式.

所以只需使用 css 而不是编写 php 脚本

于 2013-03-13T18:42:10.147 回答
0

如果您稍后需要数据/元素,例如您计划使用 JavaScript 来显示它:

CSS 是一种可接受的解决方案,您可以使用 Javascript 轻松更改类。

如果以后不需要:

通过 PHP “隐藏”很好,因为从技术上讲,它永远不会在 HTML 中呈现。

于 2013-03-13T18:44:05.360 回答
0

这取决于您的需求。一般来说,我会说如果页面上永远不需要它(即,它不是基于任何参数或用户操作动态显示/隐藏),那么根本不要发送它。对我来说,动态生成其他代码的代码(这是 PHP 对 HTML 所做的)最干净的输出没有包含任何不必要的内容。

于 2013-03-13T18:34:02.267 回答