【问题标题】:dynamically change of Navigation bar in codeigniter在codeigniter中动态改变导航栏
【发布时间】:2018-07-03 17:44:59
【问题描述】:

我正在使用带有 CodeIgniter 的引导导航栏。 现在在我的应用程序中,我想将 <li> 类更改为动态地为每个页面活动。 所以我可以知道我目前在哪个页面上。这工作正常,但我面临的问题是当我点击 任何其他链接,如“关于我们”或“服务”等。它应该在关于我们或服务等上显示为活跃的,并且 不应在主页上显示为活动,但在我的情况下,它显示为关于我们或服务页面等的活动,但 除此之外,它还在主页上显示活动页面,甚至不在主页上显示活跃页面,它应该只显示活动页面 关于我们或这样的服务的页面,请谁能告诉我如何解决这个问题。

Home.php(控制器页面中的文件)

    <?php  
    defined('BASEPATH') OR exit('No direct script access allowed');  

    class Home extends CI_Controller {  

        public function index()  
        {  
            $this->load->helper('url');
            $this->load->view('header');
            $this->load->view('index'); 
            $this->load->view('footer');
        }  

        public function about()  
        {  
        $this->load->helper('url');
        $this->load->view('header');
        $this->load->view('about'); 
        $this->load->view('footer');                    
        }

        public function services()  
        {  
        $this->load->helper('url');
        $this->load->view('header');
        $this->load->view('services'); 
        $this->load->view('footer');                    
        }

        public function portfolio()  
        {  
        $this->load->helper('url');
        $this->load->view('header');
        $this->load->view('portfolio'); 
        $this->load->view('footer');                    
        }

        public function blog()  
        {  
        $this->load->helper('url');
        $this->load->view('header');
        $this->load->view('blog'); 
        $this->load->view('footer');                    
        }

        public function contact()  
        {  
        $this->load->helper('url');
        $this->load->view('header');
        $this->load->view('contact'); 
        $this->load->view('footer');                    
        }
    }  
    ?>

header.php(查看页面中的文件)

    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation"><a href="<?php echo site_url("Home/"); ?>" class="<?php if($this->uri->segment(1)=="Home"){echo 'active';}?>">Home</a></li> 
        <li role="presentation"><a href="<?php echo site_url("Home/About/"); ?>" class="<?php if($this->uri->segment(2)=="About"){echo 'active';}?>">About Us</a></li>
        <li role="presentation"><a href="<?php echo site_url("Home/Services/"); ?>" class="<?php if($this->uri->segment(2)=="Services"){echo 'active';}?>">Services</a></li>
        <li role="presentation"><a href="<?php echo site_url("Home/Portfolio/"); ?>" class="<?php if($this->uri->segment(2)=="Portfolio"){echo 'active';}?>">Portfolio</a></li>
        <li role="presentation"><a href="<?php echo site_url("Home/Blog/"); ?>" class="<?php if($this->uri->segment(2)=="Blog"){echo 'active';}?>">Blog</a></li>
        <li role="presentation"><a href="<?php echo site_url("Home/Contact/"); ?>" class="<?php if($this->uri->segment(2)=="Contact"){echo 'active';}?>">Contact</a></li>
    </ul>

【问题讨论】:

    标签: php html css


    【解决方案1】:

    这是因为“家”始终是您结构中的第 1 段,因此它始终处于活动状态。

    像这样改变逻辑:

        <li role="presentation"><a href="<?php echo site_url("Home/"); ?>" class="<?php if(is_null($this->uri->segment(2)){echo 'active';}?>">Home</a></li> 
    

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 2018-03-22
      相关资源
      最近更新 更多