Getting Your Feet Wet With jQuery

866 views

Published on

Presentation on jQuery focusing on Selectors, Caching, and Traversing

Published in: Technology, Education, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
866
On SlideShare
0
From Embeds
0
Number of Embeds
252
Actions
Shares
0
Downloads
37
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Getting Your Feet Wet With jQuery

  1. 1. Getting your feet wet with jQuery Benjamin Sterling Twitter: bmsterling | AIM: thekenzoco | Skype: benjamin.sterling
  2. 2. Why jQuery? •DOM scripting without thinking •Cross browser support •A philosophy that makes sense •Small footprint •A great community •Everyone is doing it
  3. 3. Our Focus •Selecting •Caching •Traversing
  4. 4. Selectors
  5. 5. Basic Selectors Do you know what CSS is? Well, that is all there is to basic selectors. Examples: •$('.ClassName') •$('#ID') •$('HtmlElement')
  6. 6. Hierarchy Selectors •ancestor descendant •parent > child •prev + next •prev ~ siblings
  7. 7. Custom Selectors :first :last :not(selector) :even :odd :eq(index) :gt(index) :lt(index) :header :animated :contains(text) :empty :input :text :password :radio :checkbox :submit :image :reset :button :file :has(selector) :parent :hidden :visible :nth-child(N) :first-child :last-child :only-child :enabled :disabled :checked :selected
  8. 8. Caching
  9. 9. Caching •Local storage of remote data designed to reduce network transfers. •Saving information learned during a previous operation to be used in future operations.
  10. 10. Caching var jqMainMenu = $('#mainmenu'); var $mainMenu = $('#mainmenu');
  11. 11. Traversing
  12. 12. Traversing •22 built in traversing methods •The ones we'll focus on •.eq() •.is() •.hasClass() •.not() •.children() •.parent() •.parents() •.siblings() •.next()/.prev() •.end()
  13. 13. .eq(N) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('#nav a').eq(1); returns <a href="#">Link 2</a> $('#nav a').eq(4) returns <a href="#">Sub Link 1</a>
  14. 14. .is(expr) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('#nav a').eq(1).is('a'); returns true $('#nav a').eq(1).is(':odd'); returns false $('#nav a').eq(1).is(':only-child'); returns false $('#nav a').eq(4).is('.active') returns true
  15. 15. .hasClass(expr) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('#nav a').eq(1).hasClass('active'); returns false $('#nav a').eq(4).hasClass('active') returns true
  16. 16. .not(expr) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('#nav a').eq(1).not('.active'); returns true $('#nav a').eq(4).not('a') returns false
  17. 17. My Family DOM
  18. 18. .children(expr) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('#nav').children() returns ???? $('#nav').children('li') returns ????
  19. 19. .children(expr) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('#nav').children() returns five child LI $('#nav').children('li') returns five child LI
  20. 20. .parent() vs .parents()
  21. 21. .parent(expr) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('.active').parent() returns LI $('.active').parent().parent() returns UL $('.active').parent().parent().parent() returns LI $('.active').parent().parent().parent().parent() returns UL#nav
  22. 22. .parents(expr) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('.active').parents() returns LI, UL, LI, UL#nav, BODY, HTML $('.active').parents('#nav') returns UL#nav
  23. 23. .siblings()
  24. 24. .siblings(expr) <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('li:eq(2)').siblings() returns 4 LI $('li:eq(2)').siblings(':not(:has(a.active))') returns 3 $ ('li:eq(2)').siblings(':not(:has(a.active))').andSelf () returns 4 LI (includes the third LI also)
  25. 25. .prev() / .next() / .end()
  26. 26. .prev() / .next() / .end() <ul id="nav"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li> <a href="#">Link 4</a> <ul> <li><a href="#" class="active">Sub Link 1</a></li> <li><a href="#">Sub Link 2</a></li> </ul> </li> <li> <a href="#">Link 3</a> <ul> <li><a href="#">Sub Link 3</a></li> <li><a href="#">Sub Link 4</a></li> </ul> </li> </ul> $('li:eq(2)').next() returns forth LI under #nav $('li:eq(2)').prev() returns second LI under #nav $('li:eq(2)').next().end().prev() returns second LI under #nav
  27. 27. closing
  28. 28. http://docs.jquery.com /
  29. 29. http://remysharp.com/jquery-api/
  30. 30. Questions? Twitter: bmsterling Skype: benjamin.sterling AIM: thekenzoco Email: benjamin.sterling@kenzomedia.com Blog: http://benjaminsterling.com

×