我遇到了与此用户类似的问题:How to add active class to codeigniter hyperlinks?
,答案是将以下内容插入.php视图页面:
<a class="<?php if($this->uri->segment(1)=="search"){echo "active";}?>" href="<?=base_url('search')?>">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
当我将此插入我的页面时,即使我的网址是&#34;搜索&#34;它没有指定班级&#34;活跃&#34;到链接标签。我试过这样做:
<?php
$uri = $this->uri->segment(1);
echo "<script type='text/javascript'>alert('$uri');</script>";
?>
并且它不发出任何警报(警报框不显示任何内容)。我也试过警告以下内容:
$this->uri->uri_string()
我得到了相同的结果(一个空的警告框)。我错过了什么?
编辑:我的控制器是Pages.php:
<?php
class Pages extends CI_Controller {
public function view($page = 'home') {
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
答案 0 :(得分:4)
试试这个
<a class="<?php if($this->uri->segment(1)=="Pages"){echo "active";}?>" href="<?=base_url('Pages')?>">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
$this->uri->segment(1)
表示www.example.com/Pages/my_Method
Base URL
= www.example.com
$this->uri->segment(1)
= Pages
$this->uri->segment(2)
= my_Method
答案 1 :(得分:2)
正如我所见,您正在尝试将当前菜单应用为class ='active'。我对你的意见是,而不是找到uri并匹配它。您可以使用以下方法控制器并查看。
<强>控制器强>
$data['active_menu'] ='search'
或
$data['active_menu'] ='home'
或
$data['active_menu'] ='help'
`$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);`
查看页面(Home_view.php / search_view.php / help_view.php)
<a class="<?php if($active_menu=="search"){echo "active";}?>" href="<?=base_url('Pages')?>">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>