Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Zero to Hero, a jQuery Primer

Zero to Hero, a jQuery Primer

Presented to Auckland Web Meetup, 15 September 2011.

Matthew Buchanan

September 15, 2011
Tweet

More Decks by Matthew Buchanan

Other Decks in Programming

Transcript

  1. CORE The core is the jQuery() function, a standard JavaScript

    function To save space, it’s aliased to $() Revert with jQuery.noConflict()
  2. Accepts CSS 2 & 3 selectors, such as $("article  >

     section") $(".menu  li:last-­‐child") $("a[href^='http://']") $("table  tr:nth-­‐child(2n+1)  td") Uses cross-browser Sizzle engine SELECTION
  3. And custom extensions, such as :visible,  :hidden,  :focus,  :disabled :eq(),

     :lt(),  :gt(),  :even,  :odd :empty,  :not(),  :has(),  :contains() :input,  :checkbox,  :radio,  :file :header,  :text,  :image CUSTOM
  4. CORE Code is generally run when DOM is ready window.onload

     =  function(){…} $(document).ready(function(){…}); Can be called repeatedly, and shortened to $(function(){…});
  5. CORE jQuery deals in ‘collections’ of one or more objects,

    so $("ul  li") returns a collection of all matching elements
  6. CORE Some JavaScript properties work with collections $("ul  li").length As

    well as array indices to isolate individual DOM nodes $("ul  li")[0]
  7. TIP When assigning jQuery collections to variables, prefix with $

    var  $myList  =  $("#mylist"); Useful reminder as to a variable’s type.
  8. CORE jQuery uses JavaScript syntax for conditional statements, loops, etc.

    if  (this  >  that)  { $("nav").hide(); }  else  {…}
  9. METHODS Now for the cool stuff. Call jQuery methods to

    manipulate objects, data and collections $("ul  li").slideDown() $("ul  li").addClass()
  10. METHODS CSS / Dimension Methods .css(),  .height(),  .width() .innerHeight(),  outerHeight()

    .innerWidth(),  .outerWidth() .offset(),  .position() and more…
  11. METHODS Traversal Methods .each(),  .find(),  .filter() .children(),  .siblings(),  .end() .first(),

     .last(),  .next(),  .prev() .parent(),  .andSelf(),  .closest() and more…
  12. METHODS Events Methods .click(),  .bind(),  .live() .focus(),  .blur(),  .hover() .mouseenter(),

     .mouseleave()   .load(),  .resize(),  .scroll() and more…
  13. METHODS if  (!document.ELEMENT_NODE)  {  document.ELEMENT_NODE  =  1;    document.ATTRIBUTE_NODE  =

     2;  document.TEXT_NODE  =  3;   document.CDATA_SECTION_NODE  =  4;  document.ENTITY_REFERENCE_NODE  =  5;   document.ENTITY_NODE  =  6;  document.PROCESSING_INSTRUCTION_NODE  =  7;   document.COMMENT_NODE  =  8;  document.DOCUMENT_NODE  =  9;   document.DOCUMENT_TYPE_NODE  =  10;  document.DOCUMENT_FRAGMENT_NODE  =  11;   document.NOTATION_NODE  =  12;  }  document._importNode  =  function(node,   allChildren)  {  switch  (node.nodeType)  {  case  document.ELEMENT_NODE:  var   newNode  =  document.createElement(node.nodeName);  if  (node.attributes  &&   node.attributes.length  >  0)  for  (var  i  =  0;  il  =  node.attributes.length; i  <  il)  newNode.setAttribute(node.attributes[i].nodeName,   node.getAttribute(node.attributes[i++].nodeName)); if  (allChildren  &&  node.childNodes  &&  node.childNodes.length  >  0) for  (var  i  =  0;  il  =  node.childNodes.length;  i  <  il)   newNode.appendChild(document._importNode(node.childNodes[i++], allChildren));  return  newNode;  break;  case  document.TEXT_NODE: case  document.CDATA_SECTION_NODE:  case  document.COMMENT_NODE: return  document.createTextNode(node.nodeValue);  break;  }  }; www.alistapart.com/articles/crossbrowserscripting
  14. METHODS As well as methods for… Array handling Forms manipulation

    File parsing Feature detection and more…
  15. CHAINING Most methods return the same jQuery collection, and can

    be chained in sequence $("div:hidden") .text("Error") .css("color","red") .fadeIn();
  16. CHAINING If a method returns a new collection, return to

    the previous collection using end() $("div").find("a") .addClass("mute") .end() .find("b").hide();
  17. CALLBACKS Some methods allow more code to be executed once

    they complete (a ‘callback’) $("div").animate( {top:  50}, function()  {…} );
  18. DEMO Given the following markup <p>…</p> how might we show

    the user a success message above the text?
  19. DEMO One solution var  message  =  "Page  saved";    

      $("<div>",  {class:  "msg"})       .text(message)       .insertBefore("p")       .hide().fadeIn();
  20. DEMO Page saved Single-origin coffee viral aesthetic, jean shorts master

    cleanse tofu yr lo-fi chambray sartorial beard +1 retro photo booth. Pitchfork williamsburg beard vinyl wes anderson. Mlkshk brooklyn portland 3 wolf moon readymade iphone food truck. Austin lomo messenger bag, mcsweeney’s gentrify tattooed vegan fixie.
  21. DEMO Page saved Single-origin coffee viral aesthetic, jean shorts master

    cleanse tofu yr lo-fi chambray sartorial beard +1 retro photo booth. Pitchfork williamsburg beard vinyl wes anderson. Mlkshk brooklyn portland 3 wolf moon readymade iphone food truck. Austin lomo messenger bag, mcsweeney’s gentrify tattooed vegan fixie.
  22. PLUGINS Plugins allow you to extend the jQuery model to

    include new methods. Galleries, lightboxes Form validation, input masks Layout control Drag & drop, sliders, calendars, etc.
  23. PLUGINS Creating your own plugin is easy $.fn.addIcon  =  function()

     { return  this.prepend( $("<span>",  {class:  "icon"}) ); }