2010-02-08 3 views
0

J'ai une liste imbriquée.
Le fichier nav-ul est masqué sauf si vous êtes sur cette page, auquel cas jQ ajoute la classe sélectionnée.
J'ai écrit un script jQuery pour gérer la mise en surbrillance de l'emplacement actuel, mais ce qui me pose problème, c'est de cacher le nav-ul lorsque vous survolez un li de niveau supérieur.
jQuery - supprime la classe de l'élément lorsque: hover

Article 1 | Article 2 | Point 3 |
Rubrique 1.1 | Article 1.2 | Article 1.3 |

<ul id="nav"> 
    <li><a href="one.html">Item 1</a> 
    <ul class="nav-ul selected"> 
     <li>Item 1.1</li> 
     <li>Item 1.2</li> 
     <li>Item 1.3</li> 
    </ul> 
    </li> 
    <li><a href="two.html">Item 2</a> 
    <ul class="nav-ul"> 
     <li>Item 2.1</li> 
     <li>Item 2.2</li> 
     <li>Item 2.3</li> 
    </ul> 
    </li> 
    <li><a href="three.html">Item 3</a> 
    <ul class="nav-ul"> 
     <li>Item 3.1</li> 
     <li>Item 3.2</li> 
     <li>Item 3.3</li> 
    </ul> 
    </li> 
</ul> 

espoir qui fait sens ...

S'il vous plaît pardonnez mon code, ceci est ma première tentative de jQuery.

$(function(){ 
    var pathFileName = (location.pathname);    // Gets the path and filename from the URL. Includes the leading slash 
                 // eg:/water/index.shtml 
// $('#alert').append(pathFileName + '<br />'); 
    var splitPath = location.pathname.split("/"); // split pathFileName at the '/' into an array 
    var i   = pathFileName.length-1; 
/* add index.shtml if pathFileName ends in slash */ 
    if (pathFileName.substr(i) == '/'){ 
     pathFileName = pathFileName + 'index.shtml'; 
    } 

    var mainSelector = "#horizontalNavigation";   // The id of the first level ul 
    var subSelector = ".nav-ul";      // The class of the second level ul 
    if (pathFileName) { 
     if (splitPath.length >= 1){ 
      var pathOnly = ""; 
      for (var i=0; i < splitPath.length-1; i++){ // Rebuild the path from the array without the filename 
       pathOnly += splitPath[i];    // eg:/water/ 
       pathOnly += "/"; 
      } 
      var fullPath = (pathOnly + 'index.shtml'); // Add index.shtml the path 

/* Fix for the Design Rainfalls bug*/ 
      if (splitPath[2] == 'designRainfalls'){  
      var pathOnly = ""; 
       for (var i=0; i < 3; i++){ 
        pathOnly += splitPath[i]; 
        pathOnly += "/"; 
       } 
      var fullPath = (pathOnly + 'index.shtml'); // Make the path /water/designRainfalls/index.shtml 
      } 
      if (fullPath != pathFileName){ 
/* Highlights the currently selected first level tab */ 
       $(mainSelector + ' a[@href^="' + pathOnly + '"]').parents('li').children('a').addClass('current'); 
/* Shows the second level nav */ 
       $(mainSelector + ' a[@href^="' + fullPath + '"]').parents('li').addClass('current'); 
      } 
     } 
/* Highlights the currently selected first level tab */ 
     $(mainSelector + ' a[@href="' + pathFileName + '"]').parents('li').children('a').addClass('current'); 
/* Shows the second level nav */ 
     $(mainSelector + ' a[@href="' + pathFileName + '"]').parents('li').addClass('current'); 
     $('.current > ul').addClass('selected'); 

/* Bold selected second level item */ 
     $('li > ul > li.current').css({fontWeight:"bold", background:"url(/water/images/l2-arrow.gif) center bottom no-repeat"}); 
/* Bold selected third level item */ 
     $('#tocBody a[@href$="' + pathFileName + '"]').parents('li').addClass('tocSelected'); 
    } 
$('.nav-ul.selected').addClass('test'); 
}); 
+0

Pouvez-vous poster le code javascript aussi? – rahul

Répondre

2

Quelque chose le long des lignes de:

var navLi = $("#nav > li"); 
navLi.hover(
    function(e) { 
     navLi.children("ul").removeClass("selected"); 
     $(e.currentTarget).children("ul").addClass("selected"); 
    } 
); 

?

+0

Merci, je vais donner un coup de feu. – StefWill

+0

Cela fait le tour pour la plupart, j'ai juste besoin de travailler sur la façon de montrer l'UL actuelle lorsque vous passez la souris à partir de l'autre premier niveau li. Merci pour cela. – StefWill

+0

@aib, ce code ne fonctionne pas dans IE ... – StefWill