Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude these elements from a jQuery selection?

My jQuery:

$("ul.dropdown ul").slideDown("slow");
$("ul.dropdown ul ul").children().hide();

That causes the ones that match the second selector to display for a brief time. How can I exclude the second set from the first set, and only show the first set?


My HTML:

    <ul class="dropdown">
        <li style="margin: 0px">
            <span id="header">
                <img src="back.gif" alt='background' style="border:none;" />                        
                <span style="position: absolute; top: 5px; left: 2px;">
                    <img src="button.gif" style="border:none;" />
                </span>                 
                <span style=" position: absolute; top: -5px; left: 70px;">
                    <p style="background-color: white; width: 200px; height: 20px; font-size: 1.2em; border: 2px solid blue">Menu</p>
                </span>
            </span>
            <ul class="sub_menu">
                 <li><a href="#">Artificial Turf</a></li>
                 <li><a href="#">blah</a></li>
                 <li id="1">
                    <a id="1.1" href="#">Batting Cages</a>
                    <ul id="2">
                        <li><a href="#">Indoor</a></li>
                        <li><a href="#">Outdoor</a></li>
                    </ul>
                 </li>
                 <li><a href="#">Benches &amp; Bleachers</a></li>
                 <li><a href="#">Communication Devices</a></li>
                 <li><a href="#">Dugouts</a></li>
                 <li><a href="#">Fencing &amp; Windscreen</a></li>
                 <li><a href="#">Floor Protectors</a></li>
                 <li><a href="#">Foul Poles</a></li>
                 <li><a href="#">Netting</a></li>
                 <li><a href="#">Outdoor Furniture &amp; Storage</a></li>
                 <li><a href="#">Outdoor Signs</a></li>
                 <li><a href="#">Padding</a></li>
                 <li><a href="#">Scoreboards</a></li>
                 <li><a href="#">Shade Structures</a></li>
                 <li><a href="#"> - VIEW ALL - </a></li>
            </ul>
        </li>
    </ul>
like image 429
Malfist Avatar asked Jun 16 '09 15:06

Malfist


People also ask

Can you use CSS selectors in jQuery for selecting elements?

jQuery uses CSS-style selectors to select parts, or elements, of an HTML page. It then lets you do something with the elements using jQuery methods, or functions. To use one of these selectors, type a dollar sign and parentheses after it: $() . This is shorthand for the jQuery() function.

How write not contain in jQuery?

$(':not(:contains('+ userString +'))'). hide(); will hide all of the elements not containing that string.


3 Answers

This is exactly what the 'not' method is for:

$('ul.dropdown ul').not('ul.dropdown ul ul').slideDown('slow');
like image 137
ozan Avatar answered Sep 30 '22 13:09

ozan


This will probably work:

$("ul.dropdown > li > ul").slideDown("slow");

">" means only direct children (http://docs.jquery.com/Selectors/child#parentchild). So as long as your heirarchy is like this (which, after your edit just now, it appears it does):

<ul>
  <li>
    <ul>
..

it will most likely work. Hopefully relevant variations are obvious...

like image 23
jwl Avatar answered Sep 30 '22 12:09

jwl


This could work.

$("ul.dropdown ul:not(ul > ul)").slideDown("slow");
$("ul.dropdown ul ul").children().hide();
like image 26
albertein Avatar answered Sep 30 '22 11:09

albertein