Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • jQuery

    1. 1. jQuery<br />Jon Erickson<br />@jonerickson<br />http://jonerickson.me/<br />http://jonerickson.me/jquery/<br />
    2. 2. What is jQuery?<br />JavaScript framework that makes your life easier<br />Selectors & Chaining<br />DOM Traversing<br />DOM Manipulation<br />Event Handling<br />Animations/Effects<br />AJAX<br />Built-in Utilities<br />Extensible<br />Tons of plug-ins built by the community for our use<br />
    3. 3. Why jQuery?<br />I’m not an expert at JavaScript<br />The people who created jQuery are!<br />Do not need to worry about cross-browser issues (all abstracted)<br />Light Weight (19 kb compressed)<br />DRY – Simple plug-in model allows for code reuse<br />Animations/Effects are simple<br />Community support is growing rapidly<br />Microsoft adoption (included in MVC template)<br />Tutorials, plug-ins, etc.<br />Client-side scripting is fun with jQuery and breathes some new life into your applications<br />Focus more on the code that solves your problem<br />Amazon, BBC, Dell, Google, IBM, NBC, Netflix, Twitter<br />
    4. 4. Sample jQuery<br />Our DOM<br />Include jQuery<br />The ‘ready event’ – Binds a function to be executed whenever the DOM is ready<br />Select the ‘toggleContent’ DOM element and bind a click event handler to it.<br />Select the ‘content’ DOM element and set the text to ‘Hello World!’<br />Prevent the default behavior of the anchor tag by returning false<br />
    5. 5. Dollar Function $();<br />JavaScript identifiers can start with $<br />The jQuery framework automatically assigns ‘$’ to the ‘jQuery’ function<br />$ === jQuerytrue<br />$(selector) is same as jQuery(selector)<br />Can use utility function to unassign $<br />$.noConflict();<br />$ === jQuery; false<br />Can reassign jQuery to another variable<br />$j = $.noConflict(); <br />$j === jQuery; true<br />$j === $; false<br />$ === jQuery; false<br />
    6. 6. Document Ready Event<br />$(document).ready(fn);<br />The bound function will be called the instant the DOM is ready to be read and manipulated.<br />As many as you want on the page.<br />Executed in the order they were added.<br />There is a shortcut: $(fn);<br />
    7. 7. Selectors<br />CSS3 Selectors<br />
    8. 8. Filters<br />$(‘div:empty’), $(‘:empty’), $(‘div:not(:empty)’)<br />Custom Filter Used<br />
    9. 9. Chaining<br />Most jQuery methods return another jQuery object (usually the same collection), which means you can chain method calls together with a fluent like syntax<br />Some methods that stop a chain, these methods return a value from the jQuery object<br /> .css(name), .text(), .html(), .val(), .height(), .width(), .is(‘:visible’)<br />Some methods will return a different jQuery collection, you can revert to the previous collection by calling .end();<br />
    10. 10. Attributes & Classes<br />Getters & Setters for attr, html, text, val<br />Getter (returns String – breaks chain)<br />var text = $(‘#myDiv’).text();<br />Setter (returns jQuery)<br />$(‘#myDiv’).text(‘Hello World!’);<br />text() escapes html()<br />val() used with inputs<br />attr() can take JSON<br />Add, Remove for Attributes & Classes<br />.removeAttr(‘someAttr’);<br />.addClass(‘someClass’);<br />.removeClass(‘someClass’);<br />.toggleClass(‘someClass’);<br />
    11. 11. Traversing<br />Family<br />parent, parents, siblings, children<br />Proximity<br />closest, next, prev, nextAll, prevAll<br />Searching<br />find<br />
    12. 12. Events<br />.bind(type, data, fn)<br /><ul><li>Binds a handler to an event on all matched elements</li></ul>.one(type, data, fn)<br /><ul><li>Binds a handler to be executed only once for each matched element</li></ul>.trigger(event, data)<br /><ul><li>Trigger an event to occur on all matched elements</li></ul>.unbind(type, fn)<br /><ul><li>Removes event handlers from all matched elements</li></ul>.live(type, fn)<br /><ul><li>Binds a handler to an event on all currently matched and future matched elements.</li></ul>.die(type, fn)<br /><ul><li>Removes a bound live event.</li></ul>.hover(fnOver, fnOut)<br /><ul><li>Interaction helper that will handle mouseover and mouseout</li></ul>.toggle(fn1, fn2, fn3, fnN, …)<br /><ul><li>Interaction helper that will toggle among two or more function calls every other click.</li></li></ul><li>Event Helpers<br /> blur, change, click, dblclick, error, focus, keydown, keypress, keyup, load, mousedown, mouseenter, mouseleave, mousemove, mouseup, resize, scroll, select, submit, unload<br />
    13. 13. Manipulation<br />Inserting Inside<br />append, prepend<br />appendTo, prependTo<br />Inserting Outside<br />after, before<br />insertAfter, insertBefore<br />Inserting Around<br />wrapInner<br />wrap<br />wrapAll<br />Replacing<br />replaceWith, replaceAll<br />Removing<br />empty, remove<br />Copying<br />clone<br />
    14. 14. Effects<br />Basics<br />show, hide, toggle<br />Sliding<br />slideUp, slideDown, slideToggle<br />Fading<br />fadeIn, fadeOut, fadeTo (opacity 0-1)<br />Custom<br />animate, stop<br />
    15. 15. Plug-ins<br />Extremely Simple – Promotes code reuse and DRY principle<br />$.fn.MyPlugin = function(params) {};<br />Return a jQuery object to prevent from “breaking the chain”<br />Unless you are returning a specific value<br />Best Practice is to wrap the plug-in declaration within an anonymous JavaScript function in order to prevent collisions with the use of $<br />Within the plug-in ‘this’ refers to the jQuery object<br />
    16. 16. AJAX<br />Helpers<br />load, get, getJSON, getScript, post (all use $.ajax behind the scenes)<br />$.ajaxSetup(options) can be used to globally set the default options for all AJAX request. You can still override these within each AJAX request object.<br />Local Events – subscribed to within AJAX request object<br />Global Events – broadcast to all elements in the DOM<br />Can be disabled within the AJAX request object’s ‘global’ property<br />.bind(‘ajaxStart’, fn) or .ajaxStart(fn)<br />G | ajaxStart<br />L | beforeSend<br />G | ajaxSend<br />L | success<br />G | ajaxSuccess<br />L | error<br />G | ajaxError<br />L | complete<br />G | ajaxComplete<br />G | ajaxStop<br />
    17. 17. Utility Functions<br />Array and Object Operations<br />$.each(object, callback) – Callback function will run for each item in the object. The each method is meant to be an immutable iterator and returns the original array.<br />$.map(array, callback) – Callback function will run for each item in the array. The map method can be used as an iterator, but is designed to be used for manipulation of the supplied array and returns a new array.<br />$.merge(first, second) – Merges the second array into the first array.<br />$.unique(array) – Removes duplicate elements (only works on arrays of DOM elements).<br />$.extend(object) – Add functions into the jQuery namespace.<br />$.extend(deep, target, object1, objectN) – Merge values from objects 1 to N into target object. Deep is an optional boolean that if true, tells jQuery to do a deep copy.<br />$.grep(array, callback, invert) – Iterates through array, and returns a new array with values from the original array that satisfy the criteria specified in the callback.<br />$.inArray(value, array) – Gets the index of the value in the array (-1 if not found).<br />String Operations<br />$.trim(str) – Removes whitespace from the given string.<br />Test Operations<br />$.isArray(obj) – Determines if the object is an array.<br />$.isFunction(obj) – Determines if the object is a function.<br />
    18. 18. jQuery Data<br />Can store data on one or more jQuery elements<br />.data(name, value)<br />value is an object<br />Retrieves data from the first element in the jQuery object<br />.data(name)<br />
    19. 19. Resources<br />jQuery Main<br />http://jquery.com<br />http://docs.jquery.com/Downloading_jQuery<br />jQuery API Documentation<br />http://api.jquery.com<br />http://docs.jquery.com<br />jQuery UI<br />http://jqueryui.com<br />http://jqueryui.com/themeroller/<br />jQuery Blog<br />http://blog.jquery.com/<br />Around The Web<br />http://stackoverflow.com<br />http://www.nettuts.com<br />http://www.smashingmagazine.com<br />Tools<br />Visual Studio JavaScript Intellisense Support (KB958502)<br />http://getfirebug.com/ (Firebug Firefox Plug-in)<br />http://jsbin.com/ (JS Bin - Collaborative JavaScript Debugging)<br />Twitter<br />@jquery, @jqueryui, @jresig, @rem, @codylindley , @reybango, @elijahmanor, @jamespadolsey, @brandonaaron, @malsup, @pbakaus, @rworth<br />