我正在使用include作为标题 而且它有这个
<li><a href="arrangements.php"><span>Arrangements</span></a></li>
<li><a href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a href="events.php"><span>Special Events</span></a></li>
我想只有当前打开的页面获得class =“current” 像这样
<li class="current"><a href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
答案 0 :(得分:1)
你可以这样做:
<?php
$menus = array();
$menus['Arrangements'] = 'arrangements.php';
$menus['Build-a-Bouquet'] = 'bouquet.php';
$menus['Special Events'] = 'events.php';
?>
<ul>
<?php
$currnet_page = $_SERVER['PHP_SELF'];
if (! empty($menus)) {
foreach ($menus as $menu => $menu_link) {
$isCurrent = ($currnet_page == $menu_link) ? 'current' : '';
?>
<li class="<?php echo $isCurrent;?>"><a href="<?php echo $menu_link;?>"><span><?php echo $menu;?</span></a></li>
<?php
}
}
?>
</ul>
答案 1 :(得分:0)
尝试这样的事情:
$active = "bouquet";
include("navigation.php");
导航:
<li<?php if( isset($active) && $active == "arrangements") echo " class='active'";?>><span>Arrangements</span></li>
... And so on.
答案 2 :(得分:0)
这只是一个简单的例子,$_SERVER['PHP_SELF']
可以完成这项工作。
<li><a href="arrangements.php"><span>Arrangements</span></a></li>
<?php
if($_SERVER['PHP_SELF'] == '/bouquet.php'){
echo '<li class="current"><a href="bouquet.php"><span>Build-a-Bouquet</span></a></li>';
}else{
echo '<li><a href="bouquet.php"><span>Build-a-Bouquet</span></a></li>';
}
?>
<li><a href="events.php"><span>Special Events</span></a></li>
答案 3 :(得分:0)
有几种方法可以做到。选择你喜欢的那个:
1) 设置你想要的$ current_ var并使用:
<li><a<? echo isset($current_arrangements) ? 'class="current"' : ''; ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a<? echo isset($current_bouquet) ? 'class="current"' : ''; ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a<? echo isset($current_special_events) ? 'class="current"' : ''; ?>href="events.php"><span>Special Events</span></a></li>
2)与上面相同,但使用一个变量并检查其值(完全按照Dark Absol写的Niet的方式)
3)将其包装到某个功能
<?
// Echo Class If Current
function ecic($page) {
echo eregi($page, $_SERVER['PHP_SELF']) ? 'class="current"' : '';
}
?>
<li><a <? ecic('arrangements'); ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a <? ecic('bouquet'); ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a <? ecic('events'); ?>href="events.php"><span>Special Events</span></a></li>
BTW:在大多数常见的PHP配置中,您只能使用<?
代替<?php
。