CodeIgniter:如何“突出显示”用户当前所在页面的链接? [英] CodeIgniter: How to 'highlight' the link of the page the user is currently on?

查看:82
本文介绍了CodeIgniter:如何“突出显示”用户当前所在页面的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CodeIgniter的新手,仍然掌握MVC方法。我只是想知道什么是解决这个最好的方法:



我的导航栏突出显示当前活动链接,如:

 < a href =index.hmlid =active>索引< / a> 
< a href =blog.hml>博客< / a>

现在,当我去blog.html时,我想id =active
通常我会为每个链接分配一个变量,然后将其值设置为'id =active'。
我不认为这是最好的方式
任何想法? / p>

更新(2012年9月12日)自从问过这个问题后,我搬到了Kohana并扩展了一个完全为此目的而创建的模块,所有我需要做的是在配置数组中指定我的菜单项,突出显示是自动进行的。模块是这里

解决方案

首先,你不应该使用id作为这种事情,id是给



代码Igniter提供了许多帮助和类,它们成为我们的工具,



$ this-> uri-> segment(n



允许您检索特定段。其中n是您要检索的段号。细分从左到右编号。例如,如果您的完整网址是这样:



http://example.com/index.php/news/local/metro/crime_is_up



区隔编号如下:


  1. 新闻

  2. 本地

  3. ://codeigniter.com/user_guide/libraries/uri.htmlrel =nofollow noreferrer> http://codeigniter.com/user_guide/libraries/uri.html



    您可以使用它来检索代表您实际在浏览器上显示的活动网页的当前网址段。


    Fairly new to CodeIgniter, still grasping the MVC approach. I'm just wondering what's the best way to solve this:

    I got my navigation bar highlighting the currently active link like so:

    <a href="index.hml" id="active">Index</a>
    <a href="blog.hml">Blog</a>
    

    Now, when I go to blog.html I want id="active" to shift accordingly. Usually I'd assign a variable to each link and then set it's value to 'id="active'. Somehow I don't think that's the best way. Any thoughts?

    Update (12. Sept 2012) Since asking this I've moved on to Kohana and expanded a module created entirely for this purpose. Now, all I need to do is specify my menu items in a config array and the highlighting happens automagically. The module is here.

    解决方案

    First of all you should not be using id for that kind of things, id is to give a unique identification number to each DOM element on the page, for what, we best use a class.

    Code Igniter provides a lot of helpers and classes that become part our tools, probably you have heard of "URL Segments" before.

    $this->uri->segment(n)

    Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:

    http://example.com/index.php/news/local/metro/crime_is_up

    The segment numbers would be this:

    1. news
    2. local
    3. metro
    4. crime_is_up

    http://codeigniter.com/user_guide/libraries/uri.html

    you can use that to retrieve the current URL segment that represents the Active Page you are actually displaying on the browser.

    这篇关于CodeIgniter:如何“突出显示”用户当前所在页面的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆