你有两种选择,你可以使用PHP和CSS,这样比较好,因为这是客户端独立的。 第二个选项是使用JavaScript和CSS
PHP & CSS
我更喜欢这种方式! 如果当前页面等于每个菜单链接,则需要检查每个站点。以下是我的解决方案的一些片段。它适用于我的网站。 我不知道它是否以你的方式进行连接:index.php/home
。认为你必须适应它。因为我使用像home.php真正的文件和blog.php的
这是menu.php,我把它给到每一个网站:
<div id="menu">
<div class="innertube">
<ul>
<?
markIfCurrent("Willkommen", "willkommen");
markIfCurrent("Salzgrotte", "salzgrotte-am-fehn");
markIfCurrent("Heilwirkung", "heilwirkung-des-salzes");
markIfCurrent("Farbtherapie", "farblichttherapie");
markIfCurrent("Salzshop", "salzshop");
markIfCurrent("Erfahrungs-<br/>berichte", "erfahrungsberichte");
markIfCurrent("Fragen und Antworten", "fragen-und-antworten");
markIfCurrent("Preise", "preise");
markIfCurrent("Galerie", "galerie");
markIfCurrent("Presse", "presse");
markIfCurrent("Praxis", "praxis");
markIfCurrent("Anfahrt", "anfahrt");
?>
</ul>
</div>
</div>
现在的PHP函数:
/* filter selected menu items */
function markIfCurrent ($name, $link) {
$should_mark = isCurrent($link);
if ($should_mark) {
$first = strtoupper(substr($name, 0, 1));
$rest = substr($name, 1);
echo "<li><span class='current'>".$name."</span></li>\n";
} else {
echo "<li><a class='lower' href='".$link."'>".$name."</a></li>\n";
}
}
/* check if $pageis current page */
function isCurrent ($page) {
/* reverse mapping */
if ($page == "willkommen") {
$page = "index";
}
$isCurrent = $page.".php" == getCurrentPage();
return $isCurrent;
}
function getCurrentPage() {
$path = $_SERVER['PHP_SELF'];
$a = explode("/", $path);
$site_name = $a[sizeof($a)-1];
return $site_name;
}
至少CSS:
li span.current a {
background-color: black; /* or #000000 */
}
li span a {
background-color: gray; /* or #cccccc*/
}
的JavaScript & CSS
这是更pseude代码,但它的PHP版本
在这种情况下,同样的逻辑也需要window.location.pathname
你得到的网址/路径名来检查当前的URL 。分割斜线并提取文件。 然后你可以遍历你的导航元素与jQuery像$("#navigation").find("a")
,然后你可以设置一个元素的css风格a.css("background-color", "black")
嘿谢谢你的答案与步骤的解释! 我已经创建了基于此的CSS,但是,当我在另一个链接(可以说,投资组合),背景没有改变。所有链接都保持灰色。 – bn60
@ user1433571您可以在[jsFiddle](http://jsfiddle.net/)等网站上发布您的代码吗?这样我们可以看到你的代码是什么样的。 – 2012-06-04 23:42:40
这是你想要的代码吗? http://jsfiddle.net/QHMSa/11/ 顺便说一句,任何想法如何设置'家'上面的框也是黑暗的? – bn60