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

19: jQuery and The DOM p1

19: jQuery and The DOM p1

Avatar for Tony Hetrick

Tony Hetrick

March 22, 2013
Tweet

More Decks by Tony Hetrick

Other Decks in Technology

Transcript

  1. Copyright Notice … The materials (used by permission) in this

    presentation are from Web Programming Step by Step written by Marty Stepp, Jessica Miller, and Victoria Kirst. † http://www.webstepbook.com/supplements-2ed.shtml … Document's original contents are © Copyright 2012 Marty Stepp, Jessica Miller, and Victoria; revised by Anthony Hetrick. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
  2. Problems with JavaScript … JavaScript is a powerful language, but

    it has many flaws: … the DOM can be clunky to use … the same code doesn't always work the same way in every browser † code that works great in Chrome, Firefox, Safari, ... will fail in IE and vice versa … many developers work around these problems with hacks (checking if browser is IE, etc.)
  3. JavaScript libraries … you can link to external JavaScript library

    files in your page … libraries expand the JS language, add features, classes, methods … some popular libraries: † jQuery, Prototype, MooTools, Google Closure, Yahoo! UI, Dojo, ExtJS, Backbone, Underscore, ... many more <script src="libraryURL" type="text/javascript"></script> HTML
  4. jQuery framework … the jQuery JavaScript library adds many useful

    features to JavaScript: † many useful extensions to the DOM
  5. jQuery framework … the jQuery JavaScript library adds many useful

    features to JavaScript: † many useful extensions to the DOM † adds utility functions for built-in types String, Array, Object, Function
  6. jQuery framework … the jQuery JavaScript library adds many useful

    features to JavaScript: † many useful extensions to the DOM † adds utility functions for built-in types String, Array, Object, Function † improves event-driven programming
  7. jQuery framework … the jQuery JavaScript library adds many useful

    features to JavaScript: † many useful extensions to the DOM † adds utility functions for built-in types String, Array, Object, Function † improves event-driven programming † many cross-browser compatibility fixes
  8. jQuery framework … the jQuery JavaScript library adds many useful

    features to JavaScript: † many useful extensions to the DOM † adds utility functions for built-in types String, Array, Object, Function † improves event-driven programming † many cross-browser compatibility fixes † makes Ajax programming easier (seen later)
  9. window.onload … We cannot use the DOM before the page

    has been constructed. jQuery gives us a more compatible way to do this. † The DOM way window.onload = function() { // use the DOM } JS
  10. window.onload … We cannot use the DOM before the page

    has been constructed. jQuery gives us a more compatible way to do this. † The DOM way window.onload = function() { // use the DOM } JS $(document).ready(function() { // use the DOM }); † The direct jQuery translation JS
  11. window.onload … We cannot use the DOM before the page

    has been constructed. jQuery gives us a more compatible way to do this. † The DOM way window.onload = function() { // use the DOM } JS $(document).ready(function() { // use the DOM }); † The direct jQuery translation JS $(function() { // use the DOM }); † The jQuery way JS
  12. Aspects of the DOM and jQuery … Identification: how do

    I obtain a reference to the node that I want.
  13. Aspects of the DOM and jQuery … Identification: how do

    I obtain a reference to the node that I want. … Traversal: how do I move around the DOM tree.
  14. Aspects of the DOM and jQuery … Identification: how do

    I obtain a reference to the node that I want. … Traversal: how do I move around the DOM tree. … Node Manipulation: how do I get or set aspects of a DOM node.
  15. Aspects of the DOM and jQuery … Identification: how do

    I obtain a reference to the node that I want. … Traversal: how do I move around the DOM tree. … Node Manipulation: how do I get or set aspects of a DOM node. … Tree Manipulation: how do I change the structure of the page.
  16. The DOM tree … The elements of a page are

    nested into a tree-like structure of objects
  17. The DOM tree … The elements of a page are

    nested into a tree-like structure of objects † half of the challenge is singling out elements that you want
  18. Selecting groups of DOM objects name description getElementById returns a

    single element matching the id getElementsByTagName returns array of descendants with the given tag, such as "div" getElementsByName returns array of descendants with the given name attribute (mostly useful for accessing form controls) querySelector * returns the first element that would be matched by the given CSS selector string querySelectorAll * returns an array of all elements that would be matched by the given CSS selector string
  19. Getting all elements of a certain type var allParas =

    document.querySelectorAll("p"); for (var i = 0; i < allParas.length; i++) { allParas[i].style.backgroundColor = "yellow"; } JS … Get all </p> tags and style them yellow <body> <p>This is the first paragraph</p> <p>This is the second paragraph</p> <p>You get the idea...</p> </body> HTML
  20. Problem's with DOM API … Not always implemented the same

    way in all browsers. // identifying a single element var list = document.getElementById("mylist"); // identifying a group of elements var specials = document.querySelectorAll("li.special"); JS
  21. Problem's with DOM API … Not always implemented the same

    way in all browsers. … Way too verbose considering the DOM is the main thing we play with. // identifying a single element var list = document.getElementById("mylist"); // identifying a group of elements var specials = document.querySelectorAll("li.special"); JS
  22. Problem's with DOM API … Not always implemented the same

    way in all browsers. … Way too verbose considering the DOM is the main thing we play with. … Why are there 5+ methods to identify elements that all behave differently and take different kinds of parameters? // identifying a single element var list = document.getElementById("mylist"); // identifying a group of elements var specials = document.querySelectorAll("li.special"); JS
  23. jQuery / DOM comparison DOM method jQuery equivalent getElementById("id") $("#id")

    getElementsByTagName("tag") $("tag") getElementsByName("somename") $("[name='somename']") querySelector("selector") $("selector") querySelectorAll("selector") $("selector")
  24. jQuery node identification // id selector ($always returns an array)

    var elem = $("#myid")[0]; // group selector var elems = $("#myid, p"); JS JS
  25. jQuery node identification // id selector ($always returns an array)

    var elem = $("#myid")[0]; // group selector var elems = $("#myid, p"); // context selector var elems = $("#myid < div p"); JS JS JS
  26. jQuery node identification // id selector ($always returns an array)

    var elem = $("#myid")[0]; // group selector var elems = $("#myid, p"); // context selector var elems = $("#myid < div p"); // complex selector var elems = $("#myid < h1.special:not(.className)"); JS JS JS JS
  27. Getting all elements of a certain type var allParas =

    document.querySelectorAll("p"); for (var i = 0; i < allParas.length; i++) { allParas[i].style.backgroundColor = "yellow"; } JS … Native JavaScript get all p element and style yellow … jQuery equivalent $("p").css("background-color", "yellow"); JS
  28. jQuery terminology … the jQuery function refers to the global

    jQuery object or the $ function depending on the context
  29. jQuery terminology … the jQuery function refers to the global

    jQuery object or the $ function depending on the context … a jQuery object the object returned by the jQuery function that often represents a group of elements
  30. jQuery terminology … the jQuery function refers to the global

    jQuery object or the $ function depending on the context … a jQuery object the object returned by the jQuery function that often represents a group of elements … selected elements the DOM elements that you have selected for, most likely by some CSS selector passed to the jQuery function and possibly later filtered further
  31. The jQuery object … The $ function always (even for

    ID selectors) returns an array-like object called a jQuery object.
  32. The jQuery object … The $ function always (even for

    ID selectors) returns an array-like object called a jQuery object. … The jQuery object wraps the originally selected DOM objects.
  33. The jQuery object … The $ function always (even for

    ID selectors) returns an array-like object called a jQuery object. … The jQuery object wraps the originally selected DOM objects. … You can access the actual DOM object by accessing the elements of the jQuery object.
  34. The jQuery object // false document.getElementById("id") == $("#id"); document.querySelectorAll("p") ==

    $("p"); // true document.getElementById("id") == $("#id")[0]; document.getElementById("id") == $("#id").get(0); document.querySelectorAll("p")[0] == $("p")[0]; JS
  35. Using $ as a wrapper … $ adds extra functionality

    to DOM elements … passing an existing DOM object to $ will give it the jQuery upgrade
  36. Using $ as a wrapper … $ adds extra functionality

    to DOM elements … passing an existing DOM object to $ will give it the jQuery upgrade // convert regular DOM objects to a jQuery object var elem = document.getElementById("myelem"); elem = $(elem); var elems = document.querySelectorAll(".special"); elems = $(elems); JS
  37. DOM context identification … You can use querySelectorAll() and querySelector()

    on any DOM object. … When you do this, it simply searches from that part of the DOM tree downward.
  38. DOM context identification … You can use querySelectorAll() and querySelector()

    on any DOM object. … When you do this, it simply searches from that part of the DOM tree downward. … Programmatic equivalent of a CSS context selector
  39. elem.find() / context parameter … jQuery gives two identical ways

    to do contextual element identification var elem = $("#myid"); // These are identical var specials = $("li.special", elem); var specials = elem.find("li.special"); JS