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>





在上述功能中,我试图使用set event创建菜单on click,但是我找不到合适的解决方案。单击主项目时需要显示子菜单。如何在不更改菜单的情况下进行修复HTML和CSS部分?

最佳答案

哥们,我想我明白了...请检查这个答案

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';
                        }
                    }
            }
        }
    });
}

10-06 14:08
查看更多