Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event.

Similar presentations


Presentation on theme: "Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event."— Presentation transcript:

1 Intro to jQuery

2 What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event handling, animations, and more "write less, do more"

3 Minification Removal of all unnecessary characters in code e.g. whitespace new line chars, and comments Reduces amount of data needed to be transferred Smaller file size = quicker page loads, but less readability A lot of tools that compress the source code for you jscompress.com is just one

4 How to Add jQuery Lastest stable version is 1.7.1 Download it and store locally Use the hosted jQuery library src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jqu ery.min.js"> src="http://ajax.microsoft.com/ajax/jquery/jquery- 1.7.1.min.js">

5 jQuery Syntax $(selector).action(); $ (typically) used to define jQuery Selector - HTML element to "query" or find Action - What to do jQuery action to perform

6 Defining jQuery $ is shorthand for the standard function (full name is jQuery) $(document).ready = jQuery(document).ready Syntactically the same Problem: '$' is used as shorthand for other JavaScript library objects There's a way to get around this: jQuery noConflict() method Ex. var jq = jQuery().noConflict(); jq(document).ready( function () { jq("p").hide(); } );

7 Event Handlers jQuery methods called when an event is "triggered" or "fired" It's common to put most jQuery functions within $ (document).ready(function) This waits until the entire page is loaded $(document).ready(function() { $("p.change").click(function() { //do something here } );

8 jQuery Selectors Allow you to manipulate/traverse HTML DOM element There are 3 types of jQuery Selectors (Think of the CSS lectures!)

9 Element Selectors jQuery uses CSS to select HTML elements $("h1") - selects all elements $("p.fname") - selects all with the class = "fname" $("h2#lname") - selects the with the id = "lname" $("#contact") - selects all elements with id = "contact" (There should only be one!) element-selectors.html

10 Attribute Selectors jQuery uses XPath to select elements with the given attributes $("[href]") - selects all elements with the href attribute $("[name = 'fname']") - selects all elements where the name attribute is equal to "fname" $("[href !='#']") - selects all elements with href attribute does NOT equal "#" attribute-selectors.html

11 CSS Selectors Changes the CSS properties of the HTML elements $(selector).css("css-property", "value"); Can pass just the property to get the current value (the first matched element is returned) Can pass multiple properties $("h1").css("color", "green") - changes the color of all h1 elements to green $("h1").css({"background-color":"yellow","font- size":"200%"}) - changes all h1 elements to have a background color yellow and font size to 200% css-selectors.html

12 jQueryHTML Manipulation jQuery gives you some methods to manipulate the DOM.addClass() - lets you add one or more classes to the element.html() - sets or returns the content (innerHTML) of a element.before() - adds content before the given element.val() - sets or gets the value of a (form) element html- manipulation.htmlhtml- manipulation.html

13 jQueryHTML Manipulation Cont. Ok cool, but what about this: You dynamically add a new element (via jQuery or some other method) and want to bind an event to it. You can use the.on() method $(parent-element-to-monitor).on("event(s)", "element- to-attach-event", eventHandler()); Ex. $(document).on( "click", "p", function(event) { alert("Cool text here!"); } ); jq-on.html

14 jQueryEffects The "old" way to do hide, show, slide, toggle, fade, and animate. (A lot of this can be done with CSS3 now). $("p#hideme").hide() - hides the p element with the id $("h1").fadeIn() - does a fade in animation to all h1 elements

15 jQuery Effects (Callbacks) cont. The callback parameter Waits to execute a function until after the parent function is executed Useful since JavaScript is a interpreted language Since JS executes line by line

16 jQuery Effects (Callbacks) cont. What's the difference between these two functions? $("p").hide(1000); alert("The paragraph is now hidden"); $("p").hide(1000,function(){ alert("The paragraph is now hidden"); } ); callback.html

17 jQuery UI Official jQuery user interface library Basically a set of useful/"official" jQuery plugins Convient UI interactions Useful widgets Cool effects Easy to use theme framework

18 Using jQuery UI Lastest stable version is 1.8.17 http://jqueryui.com/download Lets you download only things you want Just have to include the.js and.css files on your pages

19 Some basic UI interactions Draggable - lets you make any DOM elements draggable Resizable - lets you resize any DOM element Selectable - makes a DOM element, or group of elements, selectable jqui-interactions.html

20 Useful widgets Datepicker - A highly configurable UI datepicker Autocomplete - Allows a Google Search like autocomplete function Button - Makes making things that aren't typically buttons be buttons Tabs - Used to break content into different sections Dialog - Similar to JS alert, but more configureable jqui-widgets.html

21 jQuery UI Effects Basically some convenience methods that extend the functionality of jQuery Animate - extends the core jQuery animate function to animate by color Hide/Show - enables animation for the effects switchClass - lets you switch from one class to another

22 jQuery UI Themes jQuery UI plugins are all styled by CSS (both the core jQuery UI style and plugin specific style) Makes it easy to keep the look and feel consistent Given the ability to create your own customized theme http://jqueryui.com/themeroller/

23 examples.html for all the examples and more resources.


Download ppt "Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event."

Similar presentations


Ads by Google