根据URL显示/隐藏Div(PHP)

时间:2011-06-01 18:41:31

标签: php html

我基本上试图在主页上隐藏DIV,但将其显示在其他地方(由URL控制)。

DIV名称“left_col”

尝试使用php执行此操作。 怎么会这样做?

1 个答案:

答案 0 :(得分:4)

假设您的首页网址为index.php,这样的内容应该可以轻松完成:

// We're NOT on the home page
if (strpos($_SERVER['REQUEST_URI'], "index.php") >= 0) {
  echo "<div id='left_col'>contents</div>";
}

还有很多其他方法可以执行此操作,具体取决于您的架构的其余部分,是否使用模板引擎或许多其他因素。如果您为问题和环境发布更多上下文,我可能会更具体。

编辑要使用CSS而不是完全抑制输出来执行此操作,此方法为每个显示和隐藏指定一个类,并将其应用于div。

// We're NOT on the home page
if (strpos($_SERVER['REQUEST_URI'], "index.php") >= 0) {
  $left_col_class = "showme";
}
else {
  $left_col_class = "hideme";
}

// Your html...
<div id='left_col' class='<?php echo $left_col_class; ?>'>contents</div>

// Your CSS
.hideme { display: none; }
.showme { display: block; }
相关问题