14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 ■ <strong>JavaScript</strong> <strong>and</strong> User Interaction: Navigation <strong>and</strong> Forms<br />

All the event listener method changeSection() needs to do is get the event target, test whether the first nested<br />

UL of the parent node has the show class applied to it, <strong>and</strong> remove that UL if that is the case. Furthermore, it needs to<br />

change the open class of the parent node to parent <strong>and</strong> vice versa.<br />

siteNavigation.js (continued)<br />

changeSection : function(e){<br />

var t = <strong>DOM</strong>help.getTarget(e);<br />

var firstList = t.parentNode.getElementsByTagName('ul')[0];<br />

if(<strong>DOM</strong>help.cssjs('check', firstList, sn.showClass)) {<br />

<strong>DOM</strong>help.cssjs('remove', firstList, sn.showClass)<br />

<strong>DOM</strong>help.cssjs('swap', t.parentNode, sn.openClass, sn.parentClass);<br />

} else {<br />

<strong>DOM</strong>help.cssjs('add', firstList,sn.showClass)<br />

<strong>DOM</strong>help.cssjs('swap', t.parentNode, sn.openClass, sn.parentClass);<br />

}<br />

<strong>DOM</strong>help.cancelClick(e);<br />

}<br />

}<br />

<strong>DOM</strong>help.addEvent(window, 'load', sn.init,false);<br />

This script, applied to the right HTML <strong>and</strong> styled <strong>with</strong> an appropriate style sheet, will give you the exp<strong>and</strong>ing <strong>and</strong><br />

collapsing navigation. The relevant parts of the CSS are as follows:<br />

siteNavigation.css (excerpt)<br />

#nav.dyn li ul{<br />

display:none;<br />

}<br />

#nav.dyn li ul.show{<br />

display:block;<br />

}<br />

#nav.dyn li{<br />

padding-left:15px;<br />

}<br />

#nav.dyn li.parent{<br />

background:url(plus.gif) 0 5px no-repeat #fff;<br />

}<br />

#nav.dyn li.open{<br />

background:url(minus.gif) 0 5px no-repeat #fff;<br />

}<br />

You can show <strong>and</strong> hide the nested UL elements by setting the values of their display properties to block <strong>and</strong><br />

none, respectively. This also takes the contained links out of the normal tabbing order: keyboard users won’t have<br />

to tab through all the links in the nested lists if they want to reach the next element on the same hierarchical level<br />

<strong>with</strong>out exp<strong>and</strong>ing the section. If they press Enter first to exp<strong>and</strong> the section, they will be able to navigate through the<br />

submenu links <strong>with</strong> the Tab key.<br />

All LI elements get a left padding to allow for an indicator image to show that the section has child links or that it<br />

is open. The LI elements <strong>with</strong> the class open or parent get a background image to indicate their state.<br />

All of this is pretty nice, but what if you want to provide a link to the parent pages of the nested sections? The<br />

solution is to add a new linked image before each of the parent links that does the showing <strong>and</strong> hiding <strong>and</strong> leaving the<br />

link as it is.<br />

www.it-ebooks.info<br />

215

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!