简体   繁体   English

通过Javascript功能设置多级菜单项的活动状态

[英]Setting active state for multi level menu items from Javascript function

I'm looking for some help with modifying an existing function which controls the highlighted states of a multi level js accordion menu. 我正在寻找有关修改现有功能的帮助,该功能可控制多级js手风琴菜单的突出显示状态。 I have had to use javascript due to certain css elements not working in safari browsers. 由于某些css元素在Safari浏览器中无法使用,我不得不使用javascript。

My problem is due to the multi level aspect as when a sub link is clicked, the parent link above it then deselects. 我的问题是由于多级方面的缘故,当单击子链接时,其上方的父链接随后被取消选择。 I need the parent link to stay active when its sub links are clicked and only deselects when a link outside of that list is clicked upon. 我需要父链接在单击其子链接时保持活动状态,并且仅在单击该列表之外的链接时才取消选择。

I understand the theory of adding a conditional statement but simply don't know how to apply it correctly within the function...any help would be very much appreciated. 我了解添加条件语句的理论,但是根本不知道如何在函数中正确应用它……任何帮助将不胜感激。

Here is the existing function which tells a link to be active or selected: 这是告诉链接处于活动或选中状态的现有函数:

var Lst;

function CngClass(obj){
if (Lst) Lst.className='.topnav';
obj.className='selected';
Lst=obj;
}

and here is the menu code: 这是菜单代码:

<ul class="topnav">
<li><a href="#">Home</a></li>

<li><a onclick="CngClass(this);" href="#">Top Link 2</a>
    <ul>
         <li><a onclick="CngClass(this);" href="#">Cookies</a></li>
         <li><a onclick="CngClass(this);" href="#">Events</a></li>
         <li><a onclick="CngClass(this);" href="#">Forms</a></li>
         <li><a onclick="CngClass(this);" href="#">Games</a></li>
         <li><a onclick="CngClass(this);" href="#">Images</a></li>
         <li><a onclick="CngClass(this);" href="#">Navigations</a>
            <ul>
                <li><a onclick="CngClass(this);" href="#">CSS</a></li>
                <li><a onclick="CngClass(this);" href="#">JavaScript</a></li>
                <li><a onclick="CngClass(this);" href="#">JQuery</a></li>
            </ul>
        </li>
         <li><a onclick="CngClass(this);" href="#">Tabs</a></li>
    </ul>
</li>
<li><a onclick="CngClass(this);" href="#">Tutorials</a>
    <ul>
         <li><a onclick="CngClass(this);" href="#">HTML</a></li>
         <li><a onclick="CngClass(this);" href="#">CSS</a></li>
         <li><a onclick="CngClass(this);" href="#">JavaScript</a></li>
         <li><a onclick="CngClass(this);" href="#">Java</a>
            <ul>
                <li><a onclick="CngClass(this);" href="#">JSP</a></li>
                <li><a onclick="CngClass(this);" href="#">JSF</a></li>
                <li><a onclick="CngClass(this);" href="#">JPA</a></li>
                <li><a onclick="CngClass(this);" href="#">Contact</a></li>
            </ul>
        </li>
         <li><a onclick="CngClass(this);" href="#">Tabs</a></li>
    </ul>
</li>
<li><a onclick="CngClass(this);" href="#">Contact</a></li>
<li><a onclick="CngClass(this);" href="#">Upload script</a></li>

Many thanks for any help or ideas... 非常感谢您的帮助或想法...

Oh dear, may I ask why you're not simply using JQuery? 哦,亲爱的,请问您为什么不简单使用JQuery? It simplifies your task so much more. 它极大地简化了您的任务。 I've got a live example with JQuery (took me about 3 mins) you can try. 我有一个关于JQuery的实时示例 (花了我大约3分钟的时间),您可以尝试。 I assume this is what you're trying to accomplish? 我认为这就是您要完成的工作?

You can use the same method in normal javascript as well, except it's more work and probably less efficient. 您也可以在普通的javascript中使用相同的方法,只是它的工作量更大且效率可能更低。 The general idea is: 总体思路是:

  1. remove active class from the current li 从当前li删除活动类
  2. find the 3rd parent from the clicked link (li, ul, li) and then apply the active class to that one instead. 从单击的链接(li,ul,li)中找到第三个父级,然后将活动类应用于该父级。

Here's the JQuery 这是jQuery

$(document).ready(function(){
    $(".topnav li a").click(function(){

        //get the ul element
        var checkElement = $(this).next();
        if(checkElement.is('ul')) {
            //check if it's visible
            if(!checkElement.is(':visible')) {
                $(this).parent().addClass('opened');
                checkElement.slideDown();
            }else{
                $(this).parent().removeClass('opened');
                checkElement.slideUp();
            }
        }       

        //get the tree node
        var parentElement = $(this).parent().parent().parent();
        if(parentElement.is('li')){
            $(".topnav li.active").removeClass('active');
            parentElement.addClass('active');
        }
    });
});

Here's an example where it highlights the li element you clicked. 这是一个示例 ,其中突出显示了您单击的li元素。

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

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