Your SlideShare is downloading. ×
Getting Your Feet Wet With jQuery
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Getting Your Feet Wet With jQuery

719
views

Published on

Presentation on jQuery focusing on Selectors, Caching, and Traversing

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
719
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
36
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Getting your feet wet with jQuery Benjamin Sterling Twitter: bmsterling | AIM: thekenzoco | Skype: benjamin.sterling
  • 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. Our Focus •Selecting •Caching •Traversing
  • 4. Selectors
  • 5. Basic Selectors Do you know what CSS is? Well, that is all there is to basic selectors. Examples: •$('.ClassName') •$('#ID') •$('HtmlElement')
  • 6. Hierarchy Selectors •ancestor descendant •parent > child •prev + next •prev ~ siblings
  • 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. Caching
  • 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. Caching var jqMainMenu = $('#mainmenu'); var $mainMenu = $('#mainmenu');
  • 11. Traversing
  • 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. .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. .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. .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. .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. My Family DOM
  • 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. .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. .parent() vs .parents()
  • 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. .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. .siblings()
  • 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. .prev() / .next() / .end()
  • 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. closing
  • 28. http://docs.jquery.com /
  • 29. http://remysharp.com/jquery-api/
  • 30. Questions? Twitter: bmsterling Skype: benjamin.sterling AIM: thekenzoco Email: benjamin.sterling@kenzomedia.com Blog: http://benjaminsterling.com

×