04 introducing jquery

23
INTRODUCING JQUERY WRITE LESS. DO MORE

Upload: rap-payne

Post on 10-May-2015

290 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 04 introducing jquery

INTRODUCING JQUERY WRITE LESS. DO MORE

Page 2: 04 introducing jquery

jQuery is a JavaScript library

Page 3: 04 introducing jquery

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

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

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

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

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

jQuery is incredibly flexible in selecting elements

$('selector')!

Page 9: 04 introducing jquery

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

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

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

Filters allow even finer-grained control •  :even and :odd •  :first and :last •  :hidden and :visible •  :not() •  :has() •  :contains()

Page 13: 04 introducing jquery

jQuery allows a fluent interface $('#popup')! .width(300)! .height(300)! .text('Hi')! .fadeIn(1000);!

Page 14: 04 introducing jquery

Let's learn to change pages

The rest of the chapter shows formulas for how to change pages.

Page 15: 04 introducing jquery

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

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

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

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

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

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

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

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

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