简体   繁体   English

从导航添加或删除.active类

[英]Add or remove .active class from navigation

I have a question I tried to remove active class from a element with jquery,I searched but did not find the problem; 我有一个问题,我试图用jquery从一个元素中删除活动类,我搜索了但没有发现问题; My html: 我的html:

<nav class="navbar-default navbar-side" role="navigation">
    <div class="sidebar-collapse" id="menu">
        <ul class="nav" id="main-menu">
            <li class="text-center">
                <img src="http://cadastru.md/assets/img/find_user.png" class="user-image img-responsive"/>
            </li>
            <li>
                <a class="active"  href="<?php echo base_url(); ?>administration/show/index"><i class="fa fa-dashboard fa-3x"></i>Home</a>
            </li>
            <li>
                <a href="<?php echo base_url();?>administration/add_user"><i class="fa fa-bar-chart-o fa-3x"></i>Add user</a>
            </li>
        </ul>
    </div>
</nav>

My jquery: 我的jQuery:

 $('.nav li a').on('click', function() {
    $('.nav li a.active').removeClass('active');
    $(this).addClass('active');
});

How to remove active class from a element? 如何从删除活动类a元素?

The problem is that you are going to a different page which means the javascript wouldn't matter, because the page changes as the javascript is changing active. 问题在于您将转到另一个页面,这意味着javascript无关紧要,因为该页面会随着javascript的活动状态变化而变化。

You need to have your nav look something like this 你需要让你的导航看起来像这样

<nav class="navbar-default navbar-side" role="navigation">
    <div class="sidebar-collapse" id="menu">
        <ul class="nav" id="main-menu">
            <li class="text-center">
                <img src="http://cadastru.md/assets/img/find_user.png" class="user-image img-responsive"/>
            </li>
            <li>
                <a class="<?php if($_SERVER['REQUEST_URI']==base_url()){echo 'active';}?>"  href="<?php echo base_url(); ?>administration/show/index"><i class="fa fa-dashboard fa-3x"></i>Home</a>
            </li>
            <li>
                <a class="<?php if($_SERVER['REQUEST_URI']==base_url() . "administration/add_user"){echo 'active';}?>" href="<?php echo base_url();?>administration/add_user"><i class="fa fa-bar-chart-o fa-3x"></i>Add user</a>
            </li>
        </ul>
    </div>
</nav>

that probably won't be perfect, but hopefully you understand the concept. 那可能不是完美的,但是希望您能理解这个概念。

Note: This wouldn't require Javascript anymore. 注意:这不再需要Javascript。

Additionally, you could add a variable called $currentPage and store a string name for the page. 此外,您可以添加一个名为$ currentPage的变量,并为该页面存储一个字符串名称。 Then you could just check if ($currentPage === "Home") {echo "active"} ... 然后,您可以只检查if ($currentPage === "Home") {echo "active"} ...

I have no idea what you are trying to do here but the following codes might help you?? 我不知道您要在这里做什么,但是以下代码可能会帮助您? The first code will toggle your active class if you want to do that (Which I assume is of no use because you are removing the same and adding the same again) and the second one removes the active class from your element. 如果您想这样做,第一个代码将切换您的活动类(我想这没有用,因为您要删除该活动类并再次添加它),而第二个代码将从您的元素中移除活动类。 Before performing anything, the code will check if your element has active class 在执行任何操作之前,代码将检查您的元素是否具有活动类

    // Toggle Class??
    $('.nav li a').click(function(event){
        event.preventDefault();
        $(this).toggleClass('active');
    });

    // Remove class??
    $('.nav li a').click(function(event){
        event.preventDefault();
        if ($(this).hasClass('active')) {
            $(this).removeClass('active');
        }
    });

Let me know if it helps you or you meant some other things 让我知道这是否对您有帮助或您有其他意思

I'm not sure, but I think that you need to remove the . 我不确定,但是我认为您需要删除. from jQuery selector, like that: 从jQuery选择器,像这样:

$('nav li a').on('click', function() {
    $('nav li a.active').removeClass('active');
    $(this).addClass('active');
});

Theoretically, without the . 理论上,没有. , the selector will find all <a> inside <li> inside <nav> ... ,选择器将在<nav>内的<li>内找到所有<a>

Take a look on jQuery documentation . 看一下jQuery文档
(Sorry for the bad english on my answer) (对不起,我的回答不好英语)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM