14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5 ■ Presentation <strong>and</strong> Behavior (CSS <strong>and</strong> Event H<strong>and</strong>ling)<br />

hover:function(e){<br />

var hl=e.target.parentNode.parentNode;<br />

var action=e.type=='mouseout'?'remove':'add';<br />

<strong>DOM</strong>help.cssjs(action,hl,newshl.overClass);<br />

}<br />

}<br />

window.addEventListener ('load',newshl.init,false);<br />

The results are clickable news headings that show associated news excerpts when you click them. The “more”<br />

links stay unaffected <strong>and</strong> will send the visitor to the full news article when clicked. (See Figure 5-6.)<br />

Figure 5-6. Exp<strong>and</strong>ing new items by clicking the headings<br />

Let’s go through the whole script step by step. After defining the CSS class properties <strong>and</strong> checking for the<br />

necessary elements, you loop through the list items, grab the first link (which is the one inside the heading), <strong>and</strong><br />

assign event listeners for click, mouseover, <strong>and</strong> mouseout. The click event should fire the newshl.toggleNews()<br />

method, while both mouseout <strong>and</strong> mouseover should trigger newshl.hover().<br />

newsItemCollapse.js (excerpt)<br />

for(i=0;i

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

Saved successfully!

Ooh no, something went wrong!