4
<style>
.menu > li:hover .sub-menu{
display:block;
}
</style>
<ul class="menu">
<li>li 1</li>
<li>li 2</li>
<li>li 3
<ul class="sub-menu">
<li>li 1</li>
<li>li 2</li>
</ul>
</li>
<li>li 4
<ul class="sub-menu">
<li>li 1</li>
<li>li 2</li>
</ul>
</li>
</ul>
<script>
var x = document.getElementsByTagName("ul");
var y = document.getElementsByTagName("li");
var i;
for(i=0;i<x.length;i++){
if(x[i].className == "sub-menu"){
x[i].style.display="none";
}
}
for(i=0;i<y.length;i++){
if(y[i].childElementCount > 0){
y[i].onclick=function(){
console.log(x[i]);
}
}
}
</script>
I think it’s a duplicate of this http://answall.com/q/1237/129, the problem is the same.
– Sergio