06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

}<br />

var li = $('');<br />

// figure out if this is first or last<br />

if (i == 0) {<br />

li.addClass('first');<br />

}<br />

else if (i == items.length - 1) {<br />

li.addClass('last');<br />

}<br />

// Add item to list<br />

li.html(menuText).appendTo(list);<br />

The highlighted portion is the new code.<br />

First, we create a <strong>jQuery</strong>-wrapped list element:<br />

var li = $('');<br />

[ 173 ]<br />

Chapter 6<br />

Once we have the li object we check it to see if it is either the first or last menu item.<br />

If it's the first, we add the first class. And if it is last, we add the last class. All<br />

others will have only the leaf class.<br />

Now that we have our list item element (li) ready, we add content <strong>and</strong> then append<br />

the entire thing to the list object:<br />

li.html(menuText).appendTo(list);<br />

By the time the for loop is done, a new li object will have been added to the list<br />

for each item that was in the original array of links.<br />

Finally, after the for loop we have one last line. We bring things together with a last<br />

<strong>jQuery</strong> chain:<br />

return list.parent().html();<br />

A theme function needs to return a string, so we grab a string representation of the<br />

main element (together with its contents) using parent().html().<br />

When all of this is put together, we should get a menu embedded in a block. The<br />

entire thing should look something like this:<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

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

Saved successfully!

Ooh no, something went wrong!