简体   繁体   English

我在Codeigniter中的侧边栏菜单活动类中需要JavaScript

[英]i need JavaScript for my sidebar menu active class in codeigniter

Hi I am new to codeigniter this is my view 嗨,我是Codeigniter的新手,这是我的观点

enter code here create_main_program"> Create Program myprogram"> My Program enter code here create_main_program“>创建程序myprogram”>我的程序

            <li class="dropdown">
                <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
                    My Profile
                        <span class="caret"></span>
                        <span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span>
                    </a>
                <ul class="dropdown-menu forAnimate" role="menu">
                        <li>
                            <a href="<?php echo  base_url(); ?>viewprofile">View Profile</a>
                        </li>
                        <li>
                            <a href="<?php echo  base_url(); ?>editprofile">Edit Profile</a>
                        </li>

                </ul>
            </li>
            <li>
                <a href="<?php echo  base_url(); ?>password">
                    Change Password
                    <span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-cog"></span>
                </a>
            </li>
            <li >
                <a href="javascript:;">
                    Premium Listing 
                    <span style="font-size:16px;" class="pull-right hidden-xs showopacity fa fa-usd"></span>
                </a>
            </li>
            <li >
                <a href="javascript:;">
                    Support
                    <span style="font-size:16px;" class="pull-right hidden-xs showopacity fa fa-question-circle"></span>
                </a>
            </li>
            <li >
                <a href="<?php echo base_url(); ?>homelogout" onclick="return confirm('Are you sure to logout?');">
                    Logout
                    <span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-off"></span>
                </a>
            </li>

        </ul>

` `

and my javascript is 而我的JavaScript是

$( document ).ready(function(){

var pathname = window.location.href;    //the url which displayed in the address bar. Ex. http://www.google.com/CreateUser
var partsArray = pathname.split('/');
var url = '/' + partsArray[4];     // after splitting the url the array will contains each part of the array starting from index 0
   alert(url);
var a_s = $('li');
a_s.removeClass('active');     //This removes active class of the previous li element
$('li a').each(function ()     // loop through all li element available in the page
{       
       // alert($(this).attr('href'));
    if ($(this).attr('href') == url)  //check whether the part of the URL and href of the current li elment is matching or not
    {
                $(this).addClass('active');   // add active class to the li element if it is matched
    } else{

    }
});   
});   

my sidebar.php is included in all pages so please help me to get sidebar selected menu active. 我的sidebar.php已包含在所有页面中,所以请帮助我激活侧边栏所选菜单。

Try to change the loop with this 尝试以此改变循环

// loop the li not a
$('li').each(function ()     // loop through all li element available in the page
{       

    // alert($(this).attr('href'));
    //assign a to variable link 
    var link = $(this).find('a');

    if (link.attr('href') == url)  //check whether the part of the URL and href of the current li elment is matching or not
    {
        link.addClass('active');   // add active class to the li element if it is matched
    } else{

    }

});  

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

相关问题 我想用javascript在我的侧边栏添加和删除活动链接 - I wanna add and remove active link on my sidebar with javascript 我的 js 脚本是在侧边栏菜单上的选择之间切换“活动”class 但只允许切换第一个选择 - My js script is to toggle "active" class between selections on a sidebar menu but only allows toggling for the first selection 包含活动菜单中的javascript菜单 - Include javascript menu with active class Javascript菜单设置类为活动状态 - Javascript Menu Setting Class to Active 将活动类动态添加到 REACT JS 中的多级侧边栏菜单 - Dynamically adding active class to multi level sidebar menu in REACT JS 将“活动”类添加到与当前单击的侧边栏菜单链接相同的href - Add 'active' class to same href as current clicked sidebar menu link 我在管理面板中有bootstrap侧边栏菜单,当我单击并获取链接时,有两个项目处于活动状态,如图片所示,为什么? - i have bootstrap Sidebar menu in my admin panel, when i click and fetch a link then two items are active , like in picture, why? 带引导侧边栏的活动类 - active class with bootstrap sidebar 显示侧边栏管理的活动菜单 - Showing active menu for sidebar adminlte JavaScript 滑动侧边栏菜单 - JavaScript Sliding Sidebar Menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM