Professional Documents
Culture Documents
Enter jQuery!
• Each JS library has its own advantages and disadvantages. Some of
the reasons that jQuery has become so popular include:
– Small size: Every time a user visits your page, along with the HTML file, CSS file(s),
images and so on that will be needed to be downloaded to their browser, they will
also need to download the library. Some versions of the jQuery library are less than
10K in size!
– Popular: We’re not taking popular in the high-school sense of the word. When it
comes to a software library, popular is good since it means that there is a large and
active community constantly testing, updating, and improving the code.
• Tested: Because of the widespread use of jQuery, bugs and other limitations are quickly
revealed and fixed.
• Plug-Ins: These are small add in programs that typically have some highly specialized
use. The large jQuery community has resulted in the development of thousands of plug
ins. As an example, the little calendar that pops up when you visit some sites and click on
a date would take quite a bit of JS progrmaming. However a single line of jQuery code
linking to the ‘Datepicker’ plug in would accomplish the same thing!
– Free: jQuery is free. Some libraries are developed by commercial software design
firms and therefore do cost money. Professionally written and maintained code
does of course have its own set of advantages, but again, you (literally) pay for it.
2
jQuery
Not the only one, but a popular one
3
4
Running jQuery
• jQuery exists as a single external JS file. You can run jQuery one of two ways:
– Download the jQuery library file to your computer
– Link to a remotely hosted version of the library code from a reliable website
• Each option has its pros and cons
– Download: The main benefit is that if you are offline, the file will still be there on your computer when you are
developing your code.
– Link: These externally linked files are typically the most up to date. However, it requires that the viewer of your page
is online. It also takes a few extra milliseconds which can, in theory, slow down your site a little bit. Still, once your
page goes ‘live’, we typically use the version linking to an external file.
– Here is a link to an article discussing these issues in more detail.
5
6
The ‘ready()’ function
Okay, let’s get started with our first jQuery function!
We will begin with a function called ‘ready()’, or more specifically,
document.ready()
• The ‘ready’ function executes after the entire web page has finished loading in the visitor’s browser. This is
a very important function to be comfortable with since much of what we do in real world JS involves
modifying content only after being certain that it has been loaded and made visible to the user on the page.
• As a result, you will typically include this function on every page in which you are using jQuery.
<script>
$(document).ready(
function()
{
// Some jQuery code will
// go in here...
}
);
</script>
I am *well* aware that this is some awkward looking code, but, never fear, all will be
explained in time!
7
Type it out!
• As always, study the file closely. Be sure you clearly understand everything that
is in the document
• I don’t expect you to completely understand the ready() function just yet. While I’m sure many
of you can see what that line is intended to do, don’t worry if you can’t. We’ll cover it in our next
lecture.
10
Aspects of the DOM and jQuery
• Identification: how do I obtain a reference to the node
that I want.
• Traversal: how do I move around the DOM tree.
• Node Manipulation: how do I get or set aspects of a
DOM node.
• Tree Manipulation: how do I change the structure of the
page.
11
Selecting Elements
Creating a “wrapped set”
• $(selector)
• selector:
– $(‘#id’) id of element
– $(‘p’) tag name
– $(‘.class’) CSS class
– $(‘p.class’) <p> elements having the CSS class
– $(‘p:first’) $(‘p:last’) $(‘p:odd’) $(‘p:even’)
– $(‘p:eq(2)’) gets the 2nd <p> element (1 based)
– $(‘p’)[1] gets the 2nd <p> element (0 based)
– $(‘p:nth-child(3)) gets the 3rd <p> element of the parent. n=even, odd too.
– $(‘p:nth-child(5n+1)’) gets the 1st element after every 5th one
– $(‘p a’) <a> elements, descended from a <p>
– $(‘p>a’) <a> elements, direct child of a <p>
– $(‘p+a’) <a> elements, directly following a <p>
– $(‘p, a’) <p> and <a> elements
– $(‘li:has(ul)’) <li> elements that have at least one <ul> descendent
– $(‘:not(p)’) all elements but <p> elements
– $(‘p:hidden’) only <p> elements that are hidden
– $(‘p:empty’) <p> elements that have no child elements
12
13
14
jQuery Selectors
Should ring a bell
15
jQuery Selectors
The easiest way to select an element yet
16
Basic Selectors
All the way back to CSS
17
Basic Selectors
All the way back to CSS
18
19
20
Animation
Hide() and Show()
21
Animation
fadeIn() and fadeOut()
22
Animation
SlideUp() and SlideDown()
25
Raw Animation
Options parameter
27
Raw Animation
Easing functions – advanced animation
• swing
easing functions.
28
Raw Animation
Easing functions – advanced animation
29
Advanced example
rotating
30
Raw Animation
Rotating
31
32
33
34
More CSS Selectors
jQuery’s selectors are powerful indeed
35
More CSS Selectors
jQuery’s selectors are powerful indeed
36
37
Attribute Selector
Really a review of CSS
39
Pseudo-Element Selector
Not to be confused with the pseudo-classes in JavaScript
41
Contextual Selector
Put it into context
43
Content Filters
Above and Beyond CSS
$("body *:contains('warning')”)
44
Form Selectors
Above and Beyond CSS
Selector CSS Equivalent Description
$("button, Selects all buttons
$(:button)
input[type='button']")
$(:checkbox) $('[type=checkbox]‘) Selects all checkboxes
$(:checked) No Equivalent Selects elements that are checked. This includes radio
buttons and checkboxes.
$(:disabled) No Equivalent Selects form elements that are disabled.
46
jQuery Attributes
And some examples
47
HTML Properties
Full circle
48
Changing CSS
With jQuery
49
Shortcut Methods
With jQuery
51
jQuery Listeners
Listener Management
52
Modifying the DOM
Creating Nodes
53
Modifying the DOM
Appending DOM Elements
54
Modifying the DOM
Prepending DOM Elements
55
Modifying the DOM
Wrapping Existing DOM in New Tags
$(".gallery").wrap('<div class="galleryLink"/>');
56
Modifying the DOM
Wrapping Existing DOM in New Tags
57
Modifying the DOM
Wrapping Existing DOM in New Tags
58