简体   繁体   English

如何在页面刷新或单击子菜单链接后激活父 ul li

[英]how to active parent ul li after page refresh or click on sub menu link

how do i keep selected tab active after page reload or refresh i have working on drop line navigation menu我如何在页面重新加载或刷新后保持所选标签处于活动状态我正在处理下拉线导航菜单

function is working fine for parent ul li and also parent active when click on parent link but problem is that when i click on sub menu link and redirect to another page then clicked parent ul li didn't active and every-time Home Tab active or highlight whenever page refresh or redirect to another page当单击父链接时,父 ul li 和父链接都可以正常工作,但问题是当我单击子菜单链接并重定向到另一个页面然后单击父 ul li 未激活并且每次Home Tab处于活动状态或每当页面刷新或重定向到另一个页面时突出显示

for example i want like this例如我想要这样

 Account Menu is parent menu and child menu like user profile and user accounts
 when i click user profile or user accounts script should active or highlight 
 Account Menu Tab Not Home Tab

Here Javascript这里Javascript

<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(function () {

        $("li:first-child").addClass("active");
        $('li').click(function () {
            $('.secondary li').removeClass('active');
            $(this).addClass('active');
        });
    })
    });//]]>  

</script>

Html Code html代码

<div role="navigation" id="navPrimary">
<ul class="secondary">
<li ><a href="#">Home</a></li>

<li><a href="#">Account Menu</a>
<ul>
<li><a href="#">OVERVIEW</a></li>
<li><a href="#">EDIT&nbsp;PROFILE</a></li>
<li><a href="#">MANAGE&nbsp;EMAILS</a></li>
<li><a href="#">EDIT&nbsp;PASSWORD</a></li>
<li><a href="#">CREDIT&nbsp;CARDS</a></li>
<li><a href="#">BANK&nbsp;ACCOUNTS</a></li>
<li><a href="#">CLOSE&nbsp;ACCOUNT</a></li>
<li><a href="#">LOGOUT</a></li>
</ul>
</li>

<li><a href="#">Payments Menu</a>
<ul>
<li><a href="#">OVERVIEW</a></li>
<li><a href="#">EDIT&nbsp;PROFILE</a></li>
<li><a href="#">MANAGE&nbsp;EMAILS</a></li>
<li><a href="#">EDIT&nbsp;PASSWORD</a></li>
<li><a href="#">CREDIT&nbsp;CARDS</a></li>
<li><a href="#">BANK&nbsp;ACCOUNTS</a></li>
<li><a href="#">CLOSE&nbsp;ACCOUNT</a></li>
<li><a href="#">LOGOUT</a></li>
</ul>

</li>
<li><a href="#">Request Money</a>

<ul>
<li><a href="">Manage Invoices</a></li>
<li><a href="" >Request Money</a></li>
<li><a href="" >Create Invoice</a></li>
<li><a href="" >Invoice Settings</a></li>
</ul>

</li>
<li><a href="#" >Merchant Services</a></li>
<li><a href="#" >Products &amp; Services</a></li>
</ul>
</div>

Updated Javascript but not working更新了 Javascript 但不起作用

<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
$(document).ready(function () {
    var index = Cookies.get('active');
    $('.secondary').find('a').removeClass('active');
    $(".secondary").find('a').eq(index).addClass('active');
    $('.secondary').on('click', 'li a', function (e) {
        e.preventDefault();
        $('.secondary').find('a').removeClass('active');
        $(this).addClass('active');
        Cookies.set('active', $('.clearfix a').index(this));
    });
});
}//]]>  

</script>

You will definitely store somewhere the data, wich was the last active tab.您肯定会将数据存储在某个地方,这是最后一个活动选项卡。 If you want to do this on client side only, one solution can be the cookies.如果您只想在客户端执行此操作,一种解决方案可以是 cookie。 See here: How do I set/unset cookie with jQuery?请参阅此处: 如何使用 jQuery 设置/取消设置 cookie?

If you are using HTML5, then you can use web storage.如果您使用的是 HTML5,那么您可以使用网络存储。 See here.看这里。

If you are rendering your page by PHP, you can use $_SESSION variable for this.如果您通过 PHP 呈现您的页面,您可以为此使用$_SESSION变量。 When user clicks a tab, you make an ajax request, and store the value of the active tab in a $_SESSION variable, but as i see, you want to do it on the client side.当用户单击选项卡时,您会发出一个 ajax 请求,并将活动选项卡的值存储在 $_SESSION 变量中,但正如我所见,您想在客户端执行此操作。

UPDATE Ok, i just created it for you.更新好的,我刚刚为你创建了它。

First thing is to set an id for every a element in my example, to know, wich is that.第一件事是在我的示例中为每个元素设置一个 id,要知道,就是这样。 You can do it some other way, but now this is the most simple.你可以用其他方式来做,但现在这是最简单的。

Second is to download the jquery.cookie.js, and do not use the URL, store it locally.二是下载jquery.cookie.js,不使用URL,本地存储。

Third, here could be a problem, if cookies are disabled on the client machine.第三,如果客户端计算机上禁用了 cookie,这可能会出现问题。

You need to do something like this:你需要做这样的事情:

HTML HTML

<div role="navigation" id="navPrimary">
    <ul class="secondary">
        <li><a href="#" id="home">Home</a></li>
        <li><a href="#" id="account">Account Menu</a>
            <ul>
                <li><a href="#" id="overview">OVERVIEW</a></li>
            </ul>
        </li>
    </ul>
</div>

CSS CSS

.active {font-weight: bold; .active {字体粗细:粗体; color: #F00}颜色:#F00}

JQUERY查询

ok, stacoverflow does not allow me to paste here a code but i loaded here the https://raw.githubusercontent.com/carhartl/jquery-cookie/master/src/jquery.cookie.js script also!好的,stacoverflow 不允许我在这里粘贴代码,但我也在这里加载了https://raw.githubusercontent.com/carhartl/jquery-cookie/master/src/jquery.cookie.js脚本!

<script type='text/javascript'>
    $(function() {
        //$.removeCookie("active");
        var activeMenu = $.cookie("active");
        console.log(activeMenu);
        if (typeof activeMenu === 'undefined') {
            $("#home").addClass("active");
        } else {
            $('#' + activeMenu).addClass('active');
        }
        $('li a').click(function(e) {
            e.preventDefault();

            var listItems = $("#navPrimary li a");
            listItems.each(function(idx, li) {
                $(li).removeClass('active');
            });

            $(this).addClass('active');
            var id = $(this).attr('id');
            $.cookie("active", id);
        });
    })
</script>

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

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