SlideShare une entreprise Scribd logo
1  sur  57
Pimp my Ride jQuery
     By @Jack_Franklin
Load from Google
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
• Always load most recent version or you
  can be more specific as you wish.
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
• Always load most recent version or you
  can be more specific as you wish.
• Chances are user may already have it
  cached.
Use of CSS SelectorS!
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
• EG: $(elem).siblings(“h2,p,strong”);
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
• EG: $(elem).siblings(“h2,p,strong”);
• Be aware of the efficiency.
Save Selectors
Save Selectors
• Urrgh:!
  $(“myelem”).hide();
  $(“myelem”).css(“width”, ”200”);
  $(“myelem”).show();
Save Selectors
• Urrgh:!
  $(“myelem”).hide();
  $(“myelem”).css(“width”, ”200”);
  $(“myelem”).show();
• Nicer:
  var elem = $(“myelem”);
  elem.hide();
  elem.css(“width”, “200”);
  elem.show();
Or Chain
Or Chain

• $(“myelem”).hide().css(“width”,
  “200”).show();
Or Chain

• $(“myelem”).hide().css(“width”,
  “200”).show();
• Either use this or caching to variables -
  whichever suits you and/or the situation
  best.
Chain over Multiple
       Lines
Chain over Multiple
         Lines
• $(“elem”).hide().addClass().show().attr();
Chain over Multiple
         Lines
• $(“elem”).hide().addClass().show().attr();
• $(“elem”)
     .hide()
     .addClass()
  //etc
Context
Context

• If you’ve cached an element, you can add
  this as context as the second parameter:
  $(“myelem”, someCachedElement);
Context

• If you’ve cached an element, you can add
  this as context as the second parameter:
  $(“myelem”, someCachedElement);
• This looks for “myelem” within
  someCachedElement.
Context Continued
Context Continued
• Contextualised selectors should be in the
  form:
  $(selector, context)
Context Continued
• Contextualised selectors should be in the
  form:
  $(selector, context)
• EG:
  $(“myelem”, “#somediv”);
  jQuery will only search inside #somediv for
  “myelem”.
Context Continued
Context Continued
• When you do use the context, jQuery just
  calls the find() method on that context:
  $(selector, context) = $
  (context).find(selector).
Context Continued
• When you do use the context, jQuery just
  calls the find() method on that context:
  $(selector, context) = $
  (context).find(selector).
• So the best way is to skip that step and just
  do:
  $(context).find(selector);
Clever Traversing




  http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.




          http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.
• Rarely should you have to go back to
  where you’ve gone:
  $
  ("elem").find("p").hide().parent().find("h2").h
  ide();


          http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.
• Rarely should you have to go back to
  where you’ve gone:
  $
  ("elem").find("p").hide().parent().find("h2").h
  ide();
• Can easily be made much nicer:
          http://api.jquery.com/category/traversing/
Clever Traversing
Clever Traversing

• Be sensible - be sure to use the right
  function for the right job.
Clever Traversing

• Be sensible - be sure to use the right
  function for the right job.
• For example - the functions find() and
  children().
Avoid Class Selection




 http://www.quirksmode.org/dom/w3c_core.html
Avoid Class Selection

• getElementById and
  getElementsByTagName are preferred to
  getElementsByClassName - supported in all
  browsers bar IE6, 7 and 8 (surprise?).




  http://www.quirksmode.org/dom/w3c_core.html
Avoid Class Selection

• getElementById and
  getElementsByTagName are preferred to
  getElementsByClassName - supported in all
  browsers bar IE6, 7 and 8 (surprise?).
• As jQuery can rely on getElementById &
  getElementsByTagName select by ID or Tag
  if you possibly can.

   http://www.quirksmode.org/dom/w3c_core.html
Check if an Element
      Exists
Check if an Element
        Exists
• Easy:
  if($(“elem”).length) {
  //element must exist
  }
Check if an Element
        Exists
• Easy:
  if($(“elem”).length) {
  //element must exist
  }
• jQuery gracefully degrades - if it does not
  exist no errors thrown and nothing breaks.
Run onLoad
Run onLoad
• Run your Javascript once the DOM is
  loaded like so (when using jQuery):
  $(document).ready(function() {
  //add code in now
  }
Run onLoad
• Run your Javascript once the DOM is
  loaded like so (when using jQuery):
  $(document).ready(function() {
  //add code in now
  }
• Shorten this to:
  $(function() {
  //code here
  }
Apply a Class to
   <body>
Apply a Class to
         <body>
• The first line of your code can add a class
  to the body to inform you if Javascript is
  on:
  $(body).addClass(“javascripton”);
Apply a Class to
         <body>
• The first line of your code can add a class
  to the body to inform you if Javascript is
  on:
  $(body).addClass(“javascripton”);
• On a similar note, if you can make CSS do
  the heavy work, do so. Javascript should do
  the bare minimum.
Store info with
jQuery’s data method
Store info with
jQuery’s data method
• jQuery provides its own method for
  storing data to be used later on.
  $(“myelem”).data(“usefulstuff”,”value of
  this data”)
Store info with
jQuery’s data method
• jQuery provides its own method for
  storing data to be used later on.
  $(“myelem”).data(“usefulstuff”,”value of
  this data”)
• And it can be retrieved like so:
  $(“myelem”).data(“usefulstuff”);
Surrender the $
Surrender the $
• jQuery.noConflict();
  Now use jQuery instead of $:
  jQuery(“myelem”).hide();
Surrender the $
• jQuery.noConflict();
  Now use jQuery instead of $:
  jQuery(“myelem”).hide();
• (function($) {
  //use $ as normal here for jQuery
  })(jQuery);
  //out here $ is not relating to jQuery
Combine & Minify


• Once you’re done, combining your scripts
  into one and then minifying them is a big
  help.
Combine & Minify


• Once you’re done, combining your scripts
  into one and then minifying them is a big
  help.
      http://www.scriptalizer.com/
Links & Resources

• Official site: www.jquery.com
• jQuery Docs: docs.jquery.com
• www.learningjquery.com
• net.tutsplus.com (hit and miss)
Thanks!
  @Jack_Franklin
www.jackfranklin.co.uk

Contenu connexe

Tendances

The Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J QueryThe Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J Query
QConLondon2008
 

Tendances (20)

jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
Jquery
JqueryJquery
Jquery
 
jQuery
jQueryjQuery
jQuery
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
An in-depth look at jQuery UI
An in-depth look at jQuery UIAn in-depth look at jQuery UI
An in-depth look at jQuery UI
 
Getting the Most Out of jQuery Widgets
Getting the Most Out of jQuery WidgetsGetting the Most Out of jQuery Widgets
Getting the Most Out of jQuery Widgets
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
jQuery
jQueryjQuery
jQuery
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
jQuery Best Practice
jQuery Best Practice jQuery Best Practice
jQuery Best Practice
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
The Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J QueryThe Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J Query
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
jQuery (MeshU)
jQuery (MeshU)jQuery (MeshU)
jQuery (MeshU)
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
 
jQuery Introduction
jQuery IntroductionjQuery Introduction
jQuery Introduction
 
jQuery
jQueryjQuery
jQuery
 

En vedette (6)

jQuery Tips & Tricks - Bath Camp 2010
jQuery Tips & Tricks - Bath Camp 2010jQuery Tips & Tricks - Bath Camp 2010
jQuery Tips & Tricks - Bath Camp 2010
 
CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better! CoffeeScript: JavaScript, but Better!
CoffeeScript: JavaScript, but Better!
 
Java script
Java scriptJava script
Java script
 
Designing for the Teenage Market
Designing for the Teenage MarketDesigning for the Teenage Market
Designing for the Teenage Market
 
Introduction to jQuery at Barcamp London 8
Introduction to jQuery at Barcamp London 8Introduction to jQuery at Barcamp London 8
Introduction to jQuery at Barcamp London 8
 
How do we prepare vocational teachers for the demands of the digital world
How do we prepare vocational teachers for the demands of the digital worldHow do we prepare vocational teachers for the demands of the digital world
How do we prepare vocational teachers for the demands of the digital world
 

Similaire à Bcblackpool jquery tips

Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3
luckysb16
 
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptxMicrosoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx
Le Hung
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
Chris Alfano
 

Similaire à Bcblackpool jquery tips (20)

J query
J queryJ query
J query
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...
 
Jquery News Packages
Jquery News PackagesJquery News Packages
Jquery News Packages
 
Jquery
JqueryJquery
Jquery
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
 
Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3
 
Jquery
JqueryJquery
Jquery
 
jQuery
jQueryjQuery
jQuery
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone Interactivity
 
Drupal Javascript for developers
Drupal Javascript for developersDrupal Javascript for developers
Drupal Javascript for developers
 
JavaScript performance patterns
JavaScript performance patternsJavaScript performance patterns
JavaScript performance patterns
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptxMicrosoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
 
jQuery-1-Ajax
jQuery-1-AjaxjQuery-1-Ajax
jQuery-1-Ajax
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
jQuery-1-Ajax
jQuery-1-AjaxjQuery-1-Ajax
jQuery-1-Ajax
 
2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx2a-JQuery AJAX.pptx
2a-JQuery AJAX.pptx
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Bcblackpool jquery tips

  • 1. Pimp my Ride jQuery By @Jack_Franklin
  • 3. Load from Google • Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js">
  • 4. Load from Google • Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js"> • Always load most recent version or you can be more specific as you wish.
  • 5. Load from Google • Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js"> • Always load most recent version or you can be more specific as you wish. • Chances are user may already have it cached.
  • 6. Use of CSS SelectorS!
  • 7. Use of CSS SelectorS! • jQuery Traversal functions take CSS Selector
  • 8. Use of CSS SelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector.
  • 9. Use of CSS SelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector. • EG: $(elem).siblings(“h2,p,strong”);
  • 10. Use of CSS SelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector. • EG: $(elem).siblings(“h2,p,strong”); • Be aware of the efficiency.
  • 12. Save Selectors • Urrgh:! $(“myelem”).hide(); $(“myelem”).css(“width”, ”200”); $(“myelem”).show();
  • 13. Save Selectors • Urrgh:! $(“myelem”).hide(); $(“myelem”).css(“width”, ”200”); $(“myelem”).show(); • Nicer: var elem = $(“myelem”); elem.hide(); elem.css(“width”, “200”); elem.show();
  • 16. Or Chain • $(“myelem”).hide().css(“width”, “200”).show(); • Either use this or caching to variables - whichever suits you and/or the situation best.
  • 18. Chain over Multiple Lines • $(“elem”).hide().addClass().show().attr();
  • 19. Chain over Multiple Lines • $(“elem”).hide().addClass().show().attr(); • $(“elem”) .hide() .addClass() //etc
  • 21. Context • If you’ve cached an element, you can add this as context as the second parameter: $(“myelem”, someCachedElement);
  • 22. Context • If you’ve cached an element, you can add this as context as the second parameter: $(“myelem”, someCachedElement); • This looks for “myelem” within someCachedElement.
  • 24. Context Continued • Contextualised selectors should be in the form: $(selector, context)
  • 25. Context Continued • Contextualised selectors should be in the form: $(selector, context) • EG: $(“myelem”, “#somediv”); jQuery will only search inside #somediv for “myelem”.
  • 27. Context Continued • When you do use the context, jQuery just calls the find() method on that context: $(selector, context) = $ (context).find(selector).
  • 28. Context Continued • When you do use the context, jQuery just calls the find() method on that context: $(selector, context) = $ (context).find(selector). • So the best way is to skip that step and just do: $(context).find(selector);
  • 29. Clever Traversing http://api.jquery.com/category/traversing/
  • 30. Clever Traversing • jQuery provides so many traversing functions that there is often a shortcut. http://api.jquery.com/category/traversing/
  • 31. Clever Traversing • jQuery provides so many traversing functions that there is often a shortcut. • Rarely should you have to go back to where you’ve gone: $ ("elem").find("p").hide().parent().find("h2").h ide(); http://api.jquery.com/category/traversing/
  • 32. Clever Traversing • jQuery provides so many traversing functions that there is often a shortcut. • Rarely should you have to go back to where you’ve gone: $ ("elem").find("p").hide().parent().find("h2").h ide(); • Can easily be made much nicer: http://api.jquery.com/category/traversing/
  • 34. Clever Traversing • Be sensible - be sure to use the right function for the right job.
  • 35. Clever Traversing • Be sensible - be sure to use the right function for the right job. • For example - the functions find() and children().
  • 36. Avoid Class Selection http://www.quirksmode.org/dom/w3c_core.html
  • 37. Avoid Class Selection • getElementById and getElementsByTagName are preferred to getElementsByClassName - supported in all browsers bar IE6, 7 and 8 (surprise?). http://www.quirksmode.org/dom/w3c_core.html
  • 38. Avoid Class Selection • getElementById and getElementsByTagName are preferred to getElementsByClassName - supported in all browsers bar IE6, 7 and 8 (surprise?). • As jQuery can rely on getElementById & getElementsByTagName select by ID or Tag if you possibly can. http://www.quirksmode.org/dom/w3c_core.html
  • 39. Check if an Element Exists
  • 40. Check if an Element Exists • Easy: if($(“elem”).length) { //element must exist }
  • 41. Check if an Element Exists • Easy: if($(“elem”).length) { //element must exist } • jQuery gracefully degrades - if it does not exist no errors thrown and nothing breaks.
  • 43. Run onLoad • Run your Javascript once the DOM is loaded like so (when using jQuery): $(document).ready(function() { //add code in now }
  • 44. Run onLoad • Run your Javascript once the DOM is loaded like so (when using jQuery): $(document).ready(function() { //add code in now } • Shorten this to: $(function() { //code here }
  • 45. Apply a Class to <body>
  • 46. Apply a Class to <body> • The first line of your code can add a class to the body to inform you if Javascript is on: $(body).addClass(“javascripton”);
  • 47. Apply a Class to <body> • The first line of your code can add a class to the body to inform you if Javascript is on: $(body).addClass(“javascripton”); • On a similar note, if you can make CSS do the heavy work, do so. Javascript should do the bare minimum.
  • 49. Store info with jQuery’s data method • jQuery provides its own method for storing data to be used later on. $(“myelem”).data(“usefulstuff”,”value of this data”)
  • 50. Store info with jQuery’s data method • jQuery provides its own method for storing data to be used later on. $(“myelem”).data(“usefulstuff”,”value of this data”) • And it can be retrieved like so: $(“myelem”).data(“usefulstuff”);
  • 52. Surrender the $ • jQuery.noConflict(); Now use jQuery instead of $: jQuery(“myelem”).hide();
  • 53. Surrender the $ • jQuery.noConflict(); Now use jQuery instead of $: jQuery(“myelem”).hide(); • (function($) { //use $ as normal here for jQuery })(jQuery); //out here $ is not relating to jQuery
  • 54. Combine & Minify • Once you’re done, combining your scripts into one and then minifying them is a big help.
  • 55. Combine & Minify • Once you’re done, combining your scripts into one and then minifying them is a big help. http://www.scriptalizer.com/
  • 56. Links & Resources • Official site: www.jquery.com • jQuery Docs: docs.jquery.com • www.learningjquery.com • net.tutsplus.com (hit and miss)

Notes de l'éditeur

  1. Point out that it&amp;#x2019;s mainly for the beginners/intermediate users. Feel free to throw in your own tips at the end.
  2. Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  3. Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  4. Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  5. Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  6. Some people like to append the dollar sign to variables that have the jQuery object &amp;#x201C;wrapped&amp;#x201D; around them.
  7. Some people like to append the dollar sign to variables that have the jQuery object &amp;#x201C;wrapped&amp;#x201D; around them.
  8. If you&amp;#x2019;re only performing multiple operations on one selector in one place, chaining is probably best. However if you plan to reuse the selector much later then cache it.
  9. If you&amp;#x2019;re only performing multiple operations on one selector in one place, chaining is probably best. However if you plan to reuse the selector much later then cache it.
  10. Completely optional - your call
  11. Completely optional - your call
  12. Just continuing from caching
  13. Just continuing from caching
  14. End with however...
  15. End with however...
  16. You can use the $(selector,context) method or .find(), choice is yours. Again, there are so many different ways of achieving the same result with jQuery
  17. You can use the $(selector,context) method or .find(), choice is yours. Again, there are so many different ways of achieving the same result with jQuery
  18. Browse the traversing docs - URL there - so many functions.
  19. Browse the traversing docs - URL there - so many functions.
  20. Browse the traversing docs - URL there - so many functions.
  21. find() will descend right through the DOM children() only descends one level
  22. find() will descend right through the DOM children() only descends one level
  23. getElementsByClassName does currently work in IE9 preview, but not in its predecessors.
  24. getElementsByClassName does currently work in IE9 preview, but not in its predecessors.
  25. Or, you can put all your jQuery at the bottom, just before the closing &lt;/body&gt; tag and not have to use this code.
  26. Or, you can put all your jQuery at the bottom, just before the closing &lt;/body&gt; tag and not have to use this code.
  27. Easy quick way to detect if javascript is on. (Most people do have it on)
  28. Easy quick way to detect if javascript is on. (Most people do have it on)
  29. Just an easy way to store things - seen people store things in alt tags, classes, etc, this is much easier.
  30. Just an easy way to store things - seen people store things in alt tags, classes, etc, this is much easier.
  31. For use with other libraries
  32. For use with other libraries
  33. A quick google search for tutorials finds you loads.