31
The jQuery JavaScript Library Hamid Zarrabi-Zadeh Web Programming – Fall 2013

Jquery

Embed Size (px)

Citation preview

Page 1: Jquery

The jQuery

JavaScript LibraryHamid Zarrabi-Zadeh

Web Programming – Fall 2013

Page 2: Jquery

Outline

• Introduction

• Using jQuery

• DOM Manipulation

• jQuery Effects

• DOM Tree Traversal

• jQuery Utilities

2

Page 3: Jquery

What is jQuery?

• A light-weight JavaScript library designed to

simplify writing JavaScript code

• The most popular library in use today, used by

over 65% of the 10,000 most visited websites

• Major alternative libraries include

Prototype

MooTools

YUI

Ext JS

3

Page 4: Jquery

jQuery Features

• jQuery Main Functionalities

DOM Manipulation

Event Handling

Effects

AJAX

Utilities

Lots of plug-ins

4

Page 5: Jquery

jQuery Pros and Cons

• jQuery Pros

write less, do more

multi-browser support

light-weight

fast and optimized

• jQuery Cons

load overhead

5

Page 6: Jquery

Using jQuery

Page 7: Jquery

Including jQuery

• To include jQuery, just load it into your page

• For a faster load, use minified and gzipped

version, from a content delivery network

7

<script src="jquery.js"></script>

<script src="http://ajax.googleapis.com/ajax/

libs/jquery/1.10.2/jquery.min.js"></script>

Page 8: Jquery

Usage Style

• With jQuery, we typically find (query) elements

and manipulate them

• Main usage style:

$(selector).action();

• Examples

8

$('p').hide();

$(document).ready(function() {

// main code

})

Page 9: Jquery

Chaining

• The output of $() function is a jQuery object

• jQuery actions are chainable, as they usually

return a jQuery object

9

$('img').addClass('test').hide();

Page 10: Jquery

Selectors

• jQuery uses CSS-like selectors

• We can select elements by tag name, id, class,

attributes, etc.

10

$('p')

$('#id')

$('.class')

$('parent > child')

$('[attr="value"]')

$('p:first')

$('tr:odd')

$(':button')

$(document.body)

Page 11: Jquery

Events

• With jQuery, we can easily attach handlers to

events

11

$('p').click(function() {

$(this).hide();

});

$('p').on('click', function() { ... });

Page 12: Jquery

DOM Manipulation

Page 13: Jquery

Get/Set Content

• jQuery methods for DOM manipulation include

text()

html()

val()

13

alert($('p').text());

$('p').html('Hello!');

Page 14: Jquery

Attributes

• You can get/set element attributes by attr()

method

14

alert($('p').attr('title'));

$('p').attr('title', 'Test');

$('a').attr({

title: 'Test',

href: 'http://example.com'

})

Page 15: Jquery

Styles

• jQuery methods for CSS manipulation include

addClass()

removeClass()

toggleClass()

css()

15

$('p').addClass('active').css('color', 'blue');

$('p').css('color'); // returns blue

Page 16: Jquery

Dimensions

• jQuery has the following dimension methods

width()

height()

innerWidth()

innerHeight()

outerWidth()

outerHeight()

16

Page 17: Jquery

jQuery Effects

Page 18: Jquery

jQuery Effects

• jQuery provides methods for basic effects,

including

hide and show

fade

slide

animate!

18

Page 19: Jquery

Hide/Show

• We can hide/show elements by hide(), show()

and toggle() methods

• The methods accept optional speed and

callback parameters

19

$('p').click(function() {

$(this).hide();

});

$('p').hide(1000, function() {

$(this).show();

});

Page 20: Jquery

Fading

• jQuery has the following fading methods

fadeIn()

fadeOut()

fadeToggle()

fadeTo()

• All with optional speed and callback parameters

20

$('p').fadeOut();

$('p').fadeOut(1000);

$('p').fadeOut('slow');

$('p').fadeTo(1000, .3);

Page 21: Jquery

Sliding

• The following sliding methods are available

slideDown()

slideUp()

slideToggle()

• With optional speed and callback parameters

21

$('p').slideUp();

$('p').slideDown(1000);

Page 22: Jquery

Animation

• Custom animations can be created with

animate() function

• You can change several styles at once

• animations are queued

22

$('p').click(function(){

$(this).animate({height: '200px'}, 'slow');

})

Page 23: Jquery

DOM Tree Traversal

Page 24: Jquery

Parents

• We can access parents of an element using:

parent()

parents()

parentsUntil()

closest()

24

$('p').parents();

$('p').parentsUntil('div');

Page 25: Jquery

Children

• We can access children of an element using:

children()

find()

25

$('p').children();

$('p').find('span');

Page 26: Jquery

Siblings

• We can access siblings of an element using:

siblings()

next()

nextAll()

nextUntil()

prev()

prevAll()

prevUntil()

26

Page 27: Jquery

Filtering

• Filtering methods

first()

last()

eq()

filter()

not()

27

$('p').first()

$('p').eq(2) // the 3rd <p> element

$('p').filter('.highlight')

$('p').not(':selected')

Page 28: Jquery

jQuery Utilities

Page 29: Jquery

Each

• The each() method iterates over a jQuery object

and executes a function for each element

• You can use $.each() to iterate over arrays and

collections

29

$('li').each(function(index) {

$(this).attr('title', 'Item '+ index);

});

$.each([2, 4, 16], function(index, value) {

alert(index + ': ' + value);

});

Page 30: Jquery

Extend

• The $.extend() method is used to merge the

contents of two or more objects

$.extend(target [, object1 ] [, objectN ])

30

var object = $.extend({}, object1, object2);

function test(params) {

var defaults = { ... };

$.extend(defaults, params);

...

}

Page 31: Jquery

References

• jQuery API Documentation

http://api.jquery.com

• W3Schools

http://www.w3schools.com/jquery

31