javascript - 我在 codeigniter 中的侧边栏菜单事件类需要 JavaScript

标签 javascript php jquery css codeigniter

嗨,我是 codeigniter 的新手,这是我的观点

在此输入代码 创建_main_program"> 创建程序 我的程序"> 我的计划

            <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>

`

我的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{

    }
});   
});   

我的 sidebar.php 包含在所有页面中,所以请帮助我激活侧边栏选择的菜单。

最佳答案

试着用这个改变循环

// 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{

    }

});  

关于javascript - 我在 codeigniter 中的侧边栏菜单事件类需要 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169387/

相关文章:

php - Wordpress - 如何在 woocommerce 中使运费为零?

PHP报错问题

php - 使用 laravel 在模型中映射字段名称

javascript - 筛选 SharePoint ListView Web 部件时尝试调用 Javascript 函数

jquery - Bootstrap js插件源代码如何 $ ('body' ).on ('focus.typeahead.data-api' , '[data-provide="typeahead"]', function (e) 工作正常吗?

php - JSON 中的关联数组解码

javascript - jQuery 结合 on() 和 addClass()

php - Functions.php wordpress 不适用于样式表或 jquery

javascript - Canvas 世界到屏幕的卡顿

javascript - React 组件用户不在状态