04 introducing jquery
TRANSCRIPT
![Page 1: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/1.jpg)
INTRODUCING JQUERY WRITE LESS. DO MORE
![Page 2: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/2.jpg)
jQuery is a JavaScript library
![Page 3: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/3.jpg)
Why jQuery? • Free and open source • Makes JavaScript sooo much easier • Everybody is doing it • Lots of plug-ins • Small file size
![Page 4: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/4.jpg)
You can get jQuery in two ways 1. CDN – Content distribution network
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> Advantages:
Less space on your site (minor) May take advantage of caching (major) Google/M$ use router tricks to serve from local servers (major)
2. Download and save <script src="scripts/jquery-1.8.3.js"></script> Advantages:
Google/M$/jQuery.com may be down (yeah, right.) Can run your site offline, like at a kiosk or tradeshow
![Page 5: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/5.jpg)
You add jQuery to a page with just a few lines of code <script type='text/javascript'>!$(document).ready(function() {! // initializations! // wiring-up of clicks and other events!});!// all other jQuery coding!</script>!
![Page 6: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/6.jpg)
Dynamically modifying a web page takes just two steps
1. Select the element(s) 2. Do something with them
• Change a property • Change the content • Remove the element • Get information from the element • Any combination of the above
![Page 7: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/7.jpg)
The DOM is a tree of elements • So how do you select one or more elements?
• Native JavaScript provides • getElementById(id); • getElementsByTagName('tag');
• Rigid • Underpowered • Tough to code
window
document
textarea
input
form
p
image
link article
image
link
div div div
link
link
![Page 8: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/8.jpg)
jQuery is incredibly flexible in selecting elements
$('selector')!
![Page 9: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/9.jpg)
Basic selectors are the simplest types • ID var theElement = $('#Id');!
• Element type var allDivs = $('div');!var allListElements = $('li');!
• Class var allOfThem = $('.className');!
![Page 10: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/10.jpg)
Advanced selectors give you a more control • Descendent
• Points to: Tag somewhere inside another tag • Example: $('#navBar a') // All links under the navBar
• Child • Points to: Tag directly beneath another tag • Example: $('#navBar > a') // All links directly within navBar
• Adjacent sibling • Points to: Tag directly adjacent to another tag • Example: $('#navBar + a') // The link right next to another
![Page 11: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/11.jpg)
Attribute selectors • Put the attribute in square brackets after the selector • Targets: elements based on the existence or value of an
attribute. • Examples: $('img[alt]') //images with the alt attribute set!$('input[type="checkbox"]') //checkboxes in the form!$('a[href^="http://"]') //all external links!$('img[src$=".png"]') //all png images!$('a[href*="google.com"]') //all links to Google!
![Page 12: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/12.jpg)
Filters allow even finer-grained control • :even and :odd • :first and :last • :hidden and :visible • :not() • :has() • :contains()
![Page 13: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/13.jpg)
jQuery allows a fluent interface $('#popup')! .width(300)! .height(300)! .text('Hi')! .fadeIn(1000);!
![Page 14: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/14.jpg)
Let's learn to change pages
The rest of the chapter shows formulas for how to change pages.
![Page 15: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/15.jpg)
html() and text() allow us to read and add content to a page //To read the html!var x = $('selector').html();!//To change the html!$('selector').html(newHtml);!//To read the inner text!var x = $('selector').text();!//To change the inner text!$('selector').text(newText);!!
![Page 16: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/16.jpg)
append() and prepend(), before() and after() allow adding content
//To add as the last child of an element!$('selector').append(newHtml);!//To add as the first child of an element!$('selector').prepend(newHtml);!//To add just after an element!$('selector').after(newHtml);!//To change immediately before an element!$('selector').before(newHtml);!!
![Page 17: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/17.jpg)
Replacing and removing selections //Take it out of the DOM!$('selector').remove();!//Replace it !$('selector').replaceWith(newContent);!//Create an exact copy of an element!var x = $('selector').clone();!x.insertAfter('selector');!
![Page 18: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/18.jpg)
Setting and reading classes
/*!Note: The classes must pre-exist. Does not destroy other classes. Merely adds this class.!*/!//Add a class!$('selector').addClass('className');!//Remove a class!$('selector').removeClass('className');!//Add if it isn't there. Remove if it is.!$('selector').toggleClass('className');!!
![Page 19: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/19.jpg)
Setting and reading CSS properties // To read a css property!var x = $(selector).css('property');!// To set a css property!$(selector).css('property','value');!// To set multiple css properties!$(selector).css(! {! 'property1':'value2',! 'property2':'value2'! }!);!
![Page 20: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/20.jpg)
Reading and setting HTML attributes • Reading an attribute var x = $(selector).attr('attributeName');!• Setting an attribute $(selector).attr('attributeName',value);!• Removing an attribute $(selector).removeAttr('attributeName');!
![Page 21: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/21.jpg)
each(function(){}); allows us to run code on each matched member in the set $('a[href^="http://"]').each(function() {//do something});!
![Page 22: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/22.jpg)
this and $(this) this $(this)
A JavaScript built-in A jQuery thing
Will point to the entire wrapped set
Will point to each thing in the wrapped set
Can be accessed any time
Can only be accessed via jQuery
![Page 23: 04 introducing jquery](https://reader033.vdocuments.net/reader033/viewer/2022042814/554f640fb4c905bb178b4938/html5/thumbnails/23.jpg)
Conclusion • jQuery is a library that helps you to write JavaScript much
faster and effectively • You copy it and link to your copy or just link to the CDN • With jQuery you can alter just about anything on a page
using selectors, which are very flexible • We saw how to do several things:
• Replacing text and html • Apply and remove CSS classes • Adding and removing HTML attributes