Author Topic: Need some JavaScript Help for HLP  (Read 662 times)

0 Members and 1 Guest are viewing this topic.

Offline Sandwich

  • Got Screen?
  • 213
    • Skype
    • Steam
    • Twitter
    • Brainzipper
Need some JavaScript Help for HLP
Ok, so I'm trying to get some cool drop-down menus to work here. They work just great in Firefox, but as usual, IE craps out due to it not supporting the :hover property on all elements.

So I use a workaround JavaScript, shown below. It parses through the child elements of the specified element ID (in this case, the "navigation" element), checking to see if they are LI elements. If they are, when the user mouses over one of these elements, the script adds the "over" class to that element, removing it when the mouse leaves the element. Then, in the appropriate CSS, I can apply CSS rules to both li:hover and li.over, solving the problem in IE.


Code: [Select]
<script language="JavaScript">
startList = function()
{
if (document.all&&document.getElementById)
{
navRoot = document.getElementById("navigation");
for (i=0; i<navRoot.childNodes.length; i++)
{
node = navRoot.childNodes[i];
if (node.nodeName=="LI")
{
node.onmouseover=function()
{
this.className+=" over";
}
node.onmouseout=function()
{
this.className=this.className.replace(" over", "");
}
}
}
}
}
window.onload=startList;
</script>

However, the script doesn't parse more than one level deep. What I want is for it to parse one more level down. Anyone here know JS enough to help out?
SERIOUSLY...! | {The Sandvich Bar} - Rhino-FS2 Tutorial | CapShip Turret Upgrade | The Complete FS2 Ship List | System Background Package

"...The quintessential quality of our age is that of dreams coming true. Just think of it. For centuries we have dreamt of flying; recently we made that come true: we have always hankered for speed; now we have speeds greater than we can stand: we wanted to speak to far parts of the Earth; we can: we wanted to explore the sea bottom; we have: and so  on, and so on: and, too, we wanted the power to smash our enemies utterly; we have it. If we had truly wanted peace, we should have had that as well. But true peace has never been one of the genuine dreams - we have got little further than preaching against war in order to appease our consciences. The truly wishful dreams, the many-minded dreams are now irresistible - they become facts." - 'The Outward Urge' by John Wyndham

"The very essence of tolerance rests on the fact that we have to be intolerant of intolerance. Stretching right back to Kant, through the Frankfurt School and up to today, liberalism means that we can do anything we like as long as we don't hurt others. This means that if we are tolerant of others' intolerance - especially when that intolerance is a call for genocide - then all we are doing is allowing that intolerance to flourish, and allowing the violence that will spring from that intolerance to continue unabated." - Bren Carlill

 

Offline Sandwich

  • Got Screen?
  • 213
    • Skype
    • Steam
    • Twitter
    • Brainzipper
Re: Need some JavaScript Help for HLP
Ok, I brute-forced it. :p Does anyone know of a more elegant way?

Code: [Select]
<script language="JavaScript">
startList = function()
{
if (document.all&&document.getElementById)
{
navRoot = document.getElementById("navigation");
for (i=0; i<navRoot.childNodes.length; i++)
{
node = navRoot.childNodes[i];
if (node.nodeName=="LI")
{
node.onmouseover=function()
{
this.className+=" over";
}
node.onmouseout=function()
{
this.className=this.className.replace(" over", "");
}
for (x=0; x<node.childNodes.length; x++)
{
subnode = node.childNodes[x];
if (subnode.nodeName=="UL")
{
for (y=0; y<subnode.childNodes.length; y++)
{
subsubnode = subnode.childNodes[y];
if (subsubnode.nodeName=="LI")
{
subsubnode.onmouseover=function()
{
this.className+=" over";
}
subsubnode.onmouseout=function()
{
this.className=this.className.replace(" over", "");
}
}
}
}
}
}
}
}
}
window.onload=startList;
</script>
SERIOUSLY...! | {The Sandvich Bar} - Rhino-FS2 Tutorial | CapShip Turret Upgrade | The Complete FS2 Ship List | System Background Package

"...The quintessential quality of our age is that of dreams coming true. Just think of it. For centuries we have dreamt of flying; recently we made that come true: we have always hankered for speed; now we have speeds greater than we can stand: we wanted to speak to far parts of the Earth; we can: we wanted to explore the sea bottom; we have: and so  on, and so on: and, too, we wanted the power to smash our enemies utterly; we have it. If we had truly wanted peace, we should have had that as well. But true peace has never been one of the genuine dreams - we have got little further than preaching against war in order to appease our consciences. The truly wishful dreams, the many-minded dreams are now irresistible - they become facts." - 'The Outward Urge' by John Wyndham

"The very essence of tolerance rests on the fact that we have to be intolerant of intolerance. Stretching right back to Kant, through the Frankfurt School and up to today, liberalism means that we can do anything we like as long as we don't hurt others. This means that if we are tolerant of others' intolerance - especially when that intolerance is a call for genocide - then all we are doing is allowing that intolerance to flourish, and allowing the violence that will spring from that intolerance to continue unabated." - Bren Carlill

  

Offline Kamikaze

  • A Complacent Wind
  • 29
    • http://www.nodewar.com
Re: Need some JavaScript Help for HLP
You could create a recursive function that goes through each level of child nodes.

Kinda like this:

Code: [Select]
define recfun(childNodes)
{
  for (i = 0; i < childNodes.length; i++)
  {
    subNode = childNodes[i];
    if (subNode.nodeName == "LI") { /* do stuff */ }
    if (subNode.childNodes.length == 0)
      continue;
    else
       recfun(subNode.childNodes);
  }
}

Note: I don't actually know the JS syntax so pardon any errors
« Last Edit: July 22, 2006, 02:29:35 pm by Kamikaze »
Science alone of all the subjects contains within itself the lesson of the danger of belief in the infallibility of the greatest teachers in the preceding generation . . .Learn from science that you must doubt the experts. As a matter of fact, I can also define science another way: Science is the belief in the ignorance of experts. - Richard Feynman

 
Re: Need some JavaScript Help for HLP
How about using getElementsByTagName?

Code: [Select]
<script language="JavaScript">
startList = function()
{
if (document.all && document.getElementById)
{
navRoot = document.getElementById("navigation");
listNodes = navRoot.getElementsByTagName("li");
for(node in listNodes)
{
node.onmouseover=function()
{
this.className+=" over";
}
node.onmouseout=function()
{
this.className=this.className.replace(" over", "");
}
}
}
}
window.onload=startList;
</script>

Best thing is, that's all crossbrowser except perhaps the for(...in...) statement, which may be IE-only.

The only Javascript coding I've done is at work, for IE. So I'm not entirely sure what's part of the standard language and what's not.

Jscript and DHTML references, courtesy of MSDN. Undoubtedly the best site for this sort of stuff, but beware MS-only 'extensions' that aren't marked as such.
« Last Edit: July 23, 2006, 05:10:31 am by Descenterace »
'And anyway, I agree - no sig images means more post, less pictures. It's annoying to sit through 40 different sigs telling about how cool, deadly, or assassin like a person is.' --Unknown Target

"You know what they say about the simplest solution."
"Bill Gates avoids it at every possible opportunity?"
-- Nuke and Colonol Drekker