Home > front end >  Why to convert js the navigation bar to comments in the code above is not an error is displayed no t
Why to convert js the navigation bar to comments in the code above is not an error is displayed no t

Time:09-29

<script>
/* window. onl oad=function () {
Var daohang=document. GetElementById (" daohang ");
The lis=daohang. GetElementsByTagName (" li ");
for(i=0; i
The lis [I]. Style. onm ouseover=function () {
Uls=lis [I] getElementsByTagName (" ul ");
Uls [0]. Style. Height="130 px"

}

Lis [I] style. The onm ouseout=function () {
Uls=lis [I] getElementsByTagName (" ul ");
Uls [0]. Style. Height="0"

}


}
} */








The function nav (liID, ulID) {
Var oLi=document. GetElementById (liID);
Var oUl=document. GetElementById (ulID);

OLi. onm ouseover=function () {
OUl. Style. Height="130 px";
};
OLi. onm ouseout=function () {
OUl. Style. Height='0';
}
}

Window. The onl oad=function () {
Nav (' li0 ', 'ul0');
Nav (' li1 ', 'ul1);
Nav (' li2 ', 'ul2');
Nav (' li3 ', 'ul3');


}

</script>
<style>

The nav {width: 430 px; height: 40px; Background: # D73033; text-align: center; line-height: 40px; list-style: none; padding: 0; }
A {color: # FFF; text-decoration: none; }
Li {width: 100 px; height: 40px; Background: # D73033; }
. Nav> Li {float: left; Margin - right: 10 px; }
. Nav> Li: last - child {margin - right: 0; }
Li> Ul {position: absolute; height: 0; overflow: hidden; The transition: height 0.5 s; list-style: none; Padding: 0; Margin: 0 px}
</style>

<body>



CodePudding user response:

Because in the event you use events outside the loop variable I,
When the event is triggered, the cycle would have ended, at that time I value is the maximum cycle plus 1,
You can put the events in the lis [I] to this

Have to use a variable I, closures can be used to save lives the value of the current cycle I
 
For (I=0; i (function (I) {
The lis [I]. Style. onm ouseover=function () {
Uls=lis [I] getElementsByTagName (" ul ");
Uls [0]. Style. Height="130 px"
}
Lis [I] style. The onm ouseout=function () {
Uls=lis [I] getElementsByTagName (" ul ");
Uls [0]. Style. Height="0"
}
}) (I);
}

CodePudding user response:

There are
The lis [I]. Style. onm ouseover
To change
The lis [I]. onm ouseover

CodePudding user response:

Window. The onl oad=function () {
Var lis=document. GetElementsByClassName (" li1 ");
Var uls=document. QuerySelectorAll (". Li1 ul ")
for(var i=0; i The lis [I] index=I;
The lis [I] onm ouseover=function () {
for(var j=0; J & lt; The lis. Length; J++) {
Uls [j] style. Height="0";
}
Uls [this index]. Style. Height="130 px";
}

The lis [I] onm ouseout=function () {
for(var j=0; J & lt; The lis. Length; J++) {
Uls [j] style. Height="0";
}

}

}
}
You try this
  • Related