【问题标题】:Using includes and stylesheets for a specific page?对特定页面使用包含和样式表?
【发布时间】:2010-12-21 20:39:07
【问题描述】:

我正要为我的网站使用包含,为我的网站设置包含 页眉、页脚、导航等。在页眉中,我将 DOCTYPE 添加到 </head> 标记的末尾。

如果我这样做,如果页面的头部在包含中,我将如何仅为一个特定页面添加样式表?

【问题讨论】:

  • DOCTYPE 需要放在 标签之前。

标签: php html css include


【解决方案1】:

在包含的头文件 php 文件中,有很多不同的方法可以做到这一点

添加一个变量

像这样

<?=@$css;?>

然后在包含它的文件中将 css 变量定义为您想要包含的任何内容

$css = 'something';

include(header.php);

如果这是你的 header.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?=@$css;?>
</head>

这是你的 index.php

$css= '<link href="style.css" rel="stylesheet" type="text/css" />';
include('header.php');

【讨论】:

  • 有点丢了,我怎么把这个添加到页面上?
【解决方案2】:

您可以在页眉上使用php来检测用户所在的页面,并显示相关的样式表。

<?php
$page = $_SERVER["REQUEST_URI"];
$clean = array("/", ".php");
$page = str_replace($clean, "", $page);
echo "
<link href=css/$page.css media=screen rel=stylesheet type=text/css />";
?>

如果你在 index.php 上,那么这个脚本会包含 index.css,main.php 会包含 main.css 等等。

【讨论】:

  • 那是相当丑陋的标题。它必须包含最少的 php 代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
相关资源
最近更新 更多