还是有更好的方法?
开关+一堆回声:
switch($currentpage) {
case 'index.php':
$indexclass=' active';
$otherpageclass='';
break;
case 'otherpage.php':
$indexclass='';
$otherpageclass=' active';
break;
}
然后,在 li 类中,我将简单地回显 $indexclass 用于索引和 $otherpageclass 用于其他页面。
另一种选择是只设置 $currentpage 变量并执行以下操作:
<li class="<?php if($currentpage='index.php'){echo ' active';}?>">whatever</li>
<li class="<?php if($currentpage='otherpage.php'){echo ' active';}?>">whatever</li>
显然,我的网站比这大得多,可能会有大约 30 个不同的菜单项,所以我想知道哪种方式最有效,或者是否有更好的方式。
先感谢您!