If you want to apply some jQuery code to an item, you use it’s selector. For example to turn on a class for all anchor tags (A) you use:
The “a” in bold is the “selector”. In this case we told jquery (which is “$”) to select all anchor tags. We then said, “with what is selected, toggle the className class on”.
If you only wanted to do that for anchor tags with the class “nav” (<a class=’nav’>) you would use:
While classes are helpful in targeting a jQuery selector, they are less efficient than using an exact ID. WordPress (I work in WP a lot) uses the ID “menu-menu” in it’s twenty eleven theme. The UL tag in WordPress for the primary nav is:
<ul id=”menu-menu” class=”menu”>
If we only wanted to target that UL, it would be best to specify the ID using a jQuery selector:
See the “#menu-menu” selector used? It says find the element who’s ID is “menu-menu” and toggle the class “activeMenu” on.
Tons more to learn, so read through the jQuery selectors page for more information. If you have any questions, post them below.
No comments yet.