tags:

views:

31

answers:

2

Hi Gurus,

I have this very simple jQuery function that allows ul.lev2 to show on hover.

$('#tab_menu li.li_level1').hover(
  function() { $('#tab_menu ul.li_level2', this).css('display', 'block'); },
  function() { $('#tab_menu ul.li_level2', this).css('display', 'none'); });

Unfortunatelly it dosent work with my html markup. I really cannot find what is going wrong.

HTML:

<div class="tab_menu menudefault" id="tab_menu">
<ul id="tab_menu_ul" class="level_1">
<li class="reserves level_1 parent parent_level_1 parent_ parent_level_1_ ">
            <span class="reserves level_1 parent parent_level_1 parent_ parent_level_1_  menuitem">123</span>
      <ul class="level_2 child_of_Reserves">
          <li class="find_a_nature_reserve level_2 parent parent_level_2 parent_ parent_level_2_ ">
              <span class="find_a_nature_reserve level_2 parent parent_level_2 parent_ parent_level_2_  menuitem">abc</span>
             </li>
          <li class="visitor_information level_2 noline child lastlev2 last">
              <a href="#" class="visitor_information level_2 noline child lastlev2 last menuitem">cde</a>
          </li>
            </ul>
        </li>
</ul>
</div>

Whenever I will remove $(this) from jQuery it seems to work, but I have more then one li tag with ul.lev2 so it is obviosly shoving all the child ul tags whenever Ill hover any parent li.

Any ideas please?

+2  A: 
$('#tab_menu ul.li_level2', this)

You're saying to select ul.li_level2 elements somewhere inside the #tabmenu, somewhere inside this. Since this is a level 1 element, you'll never find #tabmenu inside this!

This is a “scoped selector”. It requires that the leftmost element in the selector (#tabmenu) be inside the context node (this), not just the rightmost element that is the one you're actually selecting.

You probably meant:

$('ul.li_level2', this)

As this is always inside #tab_menu you don't need to check for that.

jQuery's context nodes always introduce a scoped selector. This is in contrast to the Selectors-API methods such as document.querySelector(), which would work with your example as the selected ul.li_level2 is both inside #tab_menu and inside this. (Selectors-API Level 2 proposes to introduce scoped selectors as a browser-level standard too but no-one has implemented it yet.)

bobince
Thank you for your answer - very helpfully. But what I have noticed is that jqury will not pick up my li.lev_1 if it is not in order as first in style. So when I have in ly li tag style="reserves level_1 parent parent_level_1 parent_ parent_level_1_" I have to change my jquery to $('#tab_menu li.reserves').hover(.. and then it works fine. Problem is that li.lev_1 is only class that it repeats itself in entire lev 1 menu.
Dom
er... are you sure you mean `style`? Looks like a `class` list to me. The order of space-separated names in a class list is irrelevant; I can assure you that `li.level_1` *will* match `<li class="foo level_1 bar">`.
bobince
yes, sorry, my mistake. It's obviously is class=""
Dom
+1  A: 

ehm.. Youre trying to access

$('ul.li_level2', this)

but i doesnt exist in you posted code. you should be finding

$('ul.level2', this)
Tokimon