简体   繁体   English

如何使用事件监听器或设置事件隐藏和显示子菜单

[英]How can i hide and display the sub-menu's on click using event listener or set event

 if ($C.getElement(".side-bar")) { $C.setEvent("click",".side-bar",function() { var x = this.childElementCount; alert(x); if ( x == 2){ var y = this.selectAllChildren.className; alert(y); } /*if(Ele.style.display == "block"){ Ele.style.display = "none"; //alert("bro...it works,you dont see me?,i'm inside if loop"); } else { Ele.style.display = "block"; //alert("bro...i'm working fine"); }*/ }); } 
 ul,li{ list-style:none; } .side-bar ul{ display :none; } 
 <html> <ul> <li class="side-bar">Menu title 1 <a> </a> <ul> <li>item 11 </li> <li>item 12 </li> <li>item 13 </li> </ul> </li> <li class="side-bar">Menu title 2 <a> </a> <ul> <li>item 21 </li> <li>item 22 </li> <li>item 23 </li> </ul> </li> </ul> </html> 

in the above function i'm trying to create a menu on click using set event ,but i cannot find out an appropriate solution.the sub menus need to be displayed when click on the main item.how can i fix it without changing the html and css part? 在上面的功能我试图创建一个on click使用设置事件的菜单,但我找不到一个合适的解决方案。点击主项目时需要显示子菜单。我可以修复它而不更改html和css部分?

buddy ,i think i got you...please check this answer 伙计,我想我有你...请检查这个答案

if ($C.getElement(".side-bar")) {
    $C.setEvent("click",".side-bar",function() {
        if ( this.childElementCount == 2){
            var nodes = this.childNodes;
            for(var i=0; i<nodes.length; i++) {
                if( nodes[i].className == 'nav nav-second-level')//if `you want to check a perticular class name paste it here`
                    {
                        if(nodes[i].style['display'] === 'block')
                            { 
                                nodes[i].style.display = 'none';
                            }
                        else{
                                nodes[i].style.display= 'block';
                        }
                    }
            }
        }
    });
}

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

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