如何添加活动类到codeigniter超链接? [英] How to add active class to codeigniter hyperlinks?
问题描述
我知道这个问题涉及很多,但我只是不能弄清楚如何做这个使用,已经回复的帖子。
I know this question comes across a lot, but I just can't figure out how to do this using the, already answered posts.
我有一个标题与导航链接。我想向当前处于活动状态的链接添加 class =active
。
I have a header with navigation links. I would like to add class="active"
to the link that's active at the moment.
我如何如果我有以下导航,请执行此操作?
How could I do this if I have the following navigation?
<nav>
<ul id="main_nav">
<li class="home">
<a href="search">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
</li>
<li class="categorie">
<a href="categorieen/all">
<i class="icon-list-ul"></i>
<span>CATEGORIE</span>
</a>
</li>
<li class="aanbieding">
<a href="aanbiedingen">
<i class="icon-shopping-cart"></i>
<span>AANBIEDING</span>
</a>
</li>
<li class="vacature">
<a href="vacatures">
<i class="icon-copy"></i>
<span>VACATURE</span>
</a>
</li>
<li class="agenda">
<a href="agenda">
<i class="icon-calendar"></i>
<span>AGENDA</span>
</a>
</li>
<li class="contact">
<a href="contact">
<i class="icon-envelope"></i>
<span>CONTACT</span>
</a>
</li>
</ul>
</nav>
我试过这个,但它不工作:
I tried this, but it did not work:
<script>
$(function() {
var href = $(this).find('a').attr('href');
alert(window.location.pathname)
if (href === window.location.pathname) {
$(this).addClass('active');
}
});
</script>
也许有更好的Codeigniter-ish方式?
Maybe there's a better Codeigniter-ish way?
推荐答案
尝试这个one.i认为不需要javascript或jquery。
try this one.i think no need of javascript or jquery.
如果你使用codeigniter, URI类别。
If you are using codeigniter then you can use URI Class.
<li class="home">
<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>
</li>
如果您遇到任何问题,请让我知道
please let me know if you face any problem
这篇关于如何添加活动类到codeigniter超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!