Download presentation
Presentation is loading. Please wait.
Published byHoward Allen Modified over 9 years ago
1
jQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved
2
Other General JS Libraries Prototype + Scriptaculous: http://script.aculo.us MooTools: http://mootools.net YUI (Yahoo User Interface): http://developer.yahoo.com/yui Dojo Toolkit: http://dojotoolkit.org 2
3
What is jQuery? Defined: The most popular general purpose JavaScript library available. It provides commonly requested functionality in a standard and cross-browser compatible fashion. Website: http://jquery.com Documentation (excellent): http://docs.jquery.com 3
4
jQuery Features Advanced element selectors Cross browser compatible DOM traversal and manipulation Display effects and animation Event handling AJAX functionality 4
5
Production vs Development Version Discussion: The only difference between the Production and Development version of jQuery is that the Production version is a “minified” copy of the Development version. The “minified” version has all comments, newlines, and extra spaces removed. Production Version: The Production version is recommended for most web sites. Because it is smaller, the download times are quicker. Development Version: If there is an error in the jQuery library, the Development version is useful for debugging because the code is on multiple lines and contains comments. 5
6
How do I use jQuery? Discussion: You include jQuery the same way you would include any external JavaScript file. Local Installation Example: <script type="text/javascript" src="js/jquery.min.js"> Content Delivery Network (CDN) Example (preferred): 6 eventViaElement.html
7
“Hello World” CSS Include Discussion: CSS code can specified inline via the tag or included via the link tag. Inline via tag: div#divBlock {... } Included via tag: <link rel="stylesheet" type="text/css" href="css/style.css"> 7 helloWorldViaJavaScript.html helloWorldViajQuery.html
8
“Hello World” CSS Content Discussion: The following JavaScript and jQuery example will use the style below for a blue DIV. Example CSS: div#divBlock { width: 300px; height: 100px; line-height: 100px; /* vertical alignment */ font-size: 50px; font-weight: bold; color: yellow; background-color: blue; text-align: center; padding: 25px; } 8 style.css
9
“Hello World” via JavaScript Discussion: The element is retrieved via getElementById() method. Then the HTML is set via the “innerHTML” property. Example: var theDiv = document.getElementById( "divBlock" ); theDiv.innerHTML = "Hello World"; 9 helloWorldViaJavaScript.html
10
“Hello World” via jQuery Discussion: The element is retrieved via “div#divBlock” selector. Then the HTML content is set via the “html” method. Example: jQuery( "div#divBlock" ).html( "Hello World" ); 10 helloWorldViajQuery.html
11
jQuery() and $() All jQuery calls use the “jQuery” object. For example: jQuery( "div#divBlock" ).html( "Hello World" ); The “$” is an alias for the “jQuery” object. For example: $( "div#divBlock" ).html( "Hello World" ); 11 helloWorldViajQuery.html
12
12 jQuery Selectors Discussion: One of the strengths of jQuery is that it has very robust element selector specification. Documentation: http://api.jquery.com/category/selectors Example: // all DIVs containing a (nested) paragraph $( "div:has(p)" ).addClass( "classy" ); $( "#idName" ).append( " " ); $( "p.class" ).attr( "title", "Call me Mr." ); $("h1:contains('John')").css("color", "red");
13
Event Handling Discussion: jQuery can also add event handlers to elements. Documentation: http://api.jquery.com/category/events Example: $("p").click( function() { alert('You clicked?'); }); 13 elementEventClick.html
14
Window Loading Discussion: The jQuery “ready()” method takes the place of the “onload” event. Example: $(document).ready( function() { $(".quote").click( function() { alert( "What's up?" ); }); 14 windowLoadViaReadyEvent.html
15
Effects Discussion: The most common effects are “hide()”, “show()”, “fadeIn()”, “fadeOut()”, “toggle()”. Documentation: http://api.jquery.com/category/effects 15
16
fadeOut() Effect Example: Click to Fade $( "#fadeDiv" ).click( function() { $( "#fadeImage" ).fadeOut( "slow", function() { alert( "Fade completed" ); // animation complete }); 16 effectsFadeOut.html
17
toggle() Effect Example: Click to Toggle $( "#toggleDiv" ).click( function() { $("#toggleImage").toggle( "slow", function() { alert( "Toggle completed" ); }); 17 effectsToggle.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.