0

I'm currently checking the users current page, and the applying a .css class to the correct menu item.

My question is, how can I do this best? Currently, I have this:

    $currentpage = $_SERVER['PHP_SELF'];
  if($currentpage == "/prefs.php") $active = "active";
  if($currentpage == "/acc.php") $active = "active";
  if($currentpage == "/forum.php") $active = "active";

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active} tipTip'>

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active} tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='{$active} tipTip'>

But this will just add the active class to all the anchor elements.

4

5 回答 5

4

简单的方法是在 HTML 中添加 if 语句:

<?php $currentpage = $_SERVER['PHP_SELF']; ?>

<a href='acc.php' title='Sync' aria-hidden='true' class='<?php if($currentpage == "/prefs.php") echo "active "; ?>tipTip'>
<a href='acc.php' title='Sync' aria-hidden='true' class='<?php if($currentpage == "/acc.php") echo "active "; ?>tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='<?php if($currentpage == "/forum.php") echo "active "; ?>tipTip'>

尽管这可能比其他选项可读性差。

于 2013-09-04T18:00:01.043 回答
2

给你的变量起不同的名字:

$currentpage = $_SERVER['PHP_SELF'];

if($currentpage == "/prefs.php") $active1 = "active";
if($currentpage == "/acc.php") $active2 = "active";
if($currentpage == "/forum.php") $active3 = "active";

<a href='acc.php' title='Sync' aria-hidden='true' class='{$active1} tipTip'>
<a href='acc.php' title='Sync' aria-hidden='true' class='{$active2} tipTip'>
<a href='forum.php' title='Statistics' aria-hidden='true' class='{$active3} tipTip'>
于 2013-09-04T17:56:04.957 回答
1
<?php
$currentpage = $_SERVER['PHP_SELF'];

echo "<a href='prefs.php' title='Sync' aria-hidden='true' class='" . ($currentpage == '/prefs.php' ? 'active' : '') . " tipTip'>
      <a href='acc.php' title='Sync' aria-hidden='true' class='" . ($currentpage == '/acc.php' ? 'active' : '') . " tipTip'>
      ...";

如果你不喜欢这个,你也可以使用一个switch() { case '': break; }设置。

于 2013-09-04T18:03:15.317 回答
0

有一种稍微不同的方法,但它也需要 JavaScript(我知道您没有在标签中列出 JavaScript,但您仍然可能会发现这很有用)。诀窍是让页面名称响应(修改的)DOM 元素 ID 或类。

// or just make a simple function that strips the name from the $currentpage
// if the names are consistent like in your example
if ($currentpage == "/prefs.php") $id="prefs"; 
elseif ($currentpage == "/acc.php") $id="acc";
elseif ($currentpage == "/forum.php") $id="forum"; 

然后,进行 JavaScript 调用以找到正确的锚元素并将“活动”类分配给它。这是一个 jQuery 示例:

var active = '<?php echo $id; ?>';
$('a .' + active).addClass('active'); // for class name
$('#' + active).addClass('active'); // for id, you get the point

这只是一个简单的示例,但我认为如果您使用 JavaScript,它可能会有所帮助并使您的代码更具可读性。

于 2013-09-04T18:04:24.843 回答
0

获取自我功能:

function getSelf($dir=""){
    if($dir==""){ $dir = $_SERVER['PHP_SELF']; }
    $self = explode("/",$dir);
    return $self[count($self)-1];
}

然后是菜单的分隔符:

<?php
                    $cls="";
                    $quick = "index.php||other_page.php||other_page2.php";
                    $stack = explode("||",$quick);
                    if(in_array(getSelf(), $stack)){ $cls = " active"; } else { $cls = ""; }
                ?>

然后class="<?php echo $cls; ?>"

于 2013-09-04T18:04:11.613 回答