0

我想在列表项中添加 class="active" 以突出显示所选菜单,但我不知道如何执行此操作,因为我不太熟悉 php。

这就是我在 tpl 文件中的内容

<ul class="menu">
<li>
    <a<?php echo $target; ?> href="<?php echo $link; ?>"<?php echo $nofollow; ?>><?php echo $this->escape_html($link_title); ?></a>
    <?php echo $sub_menu; ?>
</li>

并在 php 文件中

        $links = $this->db->GetAll($query);
    foreach($links as $link) {
        $template = $this->PMDR->getNew('Template');
        $template->set('link',$link['url']);
        $template->set('link_title',$link['title']);
        $template->set('nofollow',($link['nofollow'] ? ' rel="'.$link['nofollow'].'"' : ''));
        $template->set('target',($link['target'] ? ' target="'.$link['target'].'"' : ''));
        $template->set('indent',str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level));
        ob_start();
        $this->getMenuLoop($link['id'], $level+1);
        $sub_menu = ob_get_clean();
        if(!empty($sub_menu)) {
            $menu_template = $this->PMDR->getNew('Template',$this->template);
            $menu_template->set('items', $sub_menu);
            $sub_menu = $menu_template->render();
        }
        $template->set('sub_menu', $sub_menu);
        echo $template->render($this->item_template);
    }
}

先感谢您!

4

1 回答 1

0

在您的 tpl 中,添加$class(我把它放在 中$target,但它可以在 中的任何地方<a>

<ul class="menu">
<li>
    <a<?php echo $target; echo $class; ?> href="<?php echo $link; ?>"<?php echo $nofollow; ?>><?php echo   $this->escape_html($link_title); ?></a>
    <?php echo $sub_menu; ?>
</li>

在你的 php 文件中添加

$current_url = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

$template->set('class',(($current_url == $link['url']) ? ' class="active"' : ''));

此检查以查看当前 url 是否与 相同$link['url'],并添加 `class="active"

    //gets the current page
    $current_url = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

    $links = $this->db->GetAll($query);
foreach($links as $link) {
    $template = $this->PMDR->getNew('Template');
    $template->set('link',$link['url']);
    $template->set('link_title',$link['title']);
    $template->set('nofollow',($link['nofollow'] ? ' rel="'.$link['nofollow'].'"' : ''));
    $template->set('target',($link['target'] ? ' target="'.$link['target'].'"' : ''));
    $template->set('class',(($current_url == $link['url']) ? ' class="active"' : ''));
    $template->set('indent',str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level));
    ob_start();
    $this->getMenuLoop($link['id'], $level+1);
    $sub_menu = ob_get_clean();
    if(!empty($sub_menu)) {
        $menu_template = $this->PMDR->getNew('Template',$this->template);
        $menu_template->set('items', $sub_menu);
        $sub_menu = $menu_template->render();
    }
    $template->set('sub_menu', $sub_menu);
    echo $template->render($this->item_template);
}

}

于 2012-09-13T22:05:07.320 回答