Jquery (Part I) : CS262. Advanced Concepts in Web Development Lecturer. Seang Lypengleang Zaman University
Jquery (Part I) : CS262. Advanced Concepts in Web Development Lecturer. Seang Lypengleang Zaman University
Jquery (Part I)
CS262. Advanced Concepts in Web Development
Lecturer. Seang Lypengleang
Zaman University
c07/js/looping.js
CHAINING
Most methods used to update
• The jQuery selection can be chained. However the methods that
retrieve information from the DOM (or about the browser) cannot be
chained.
c07/js/chaining.js
Checking a Page is Ready to Work with
Checking a Page is Ready to Work with
Getting Element Content
• The •html() and •text() methods retrieve and update the content of elements.
• .html() retrieves only the HTML inside the first element in the matched set,
along with any of its descendants.
• For example, $ ('ul')•html(); will return this:
<li id="one"><em>fresh</em> figs</li>
<li id="two">pine nuts</li>
<li id="three">honey</li>
<li id="four">balsamic vinegar</li>
• Whereas $('li').html(); will return this:
<em>fresh</em> figs
• This returns only the content of the first <li> element.
• If wanting to retrieve the value of every element, use the .each() method.
Getting Element Content
• .text() returns the content from every element in the jQuery selection,
along with the text from any descendants.
• For example, $('ul').text(); will return this:
fresh figs
pine nuts
honey
Balsamic vinegar
• Whereas $('li').text(); will return this:
fresh figspine nutshoneybalsamic vinegar
• This returns the text content of all <li> elements (spaces between words),
but there are no spaces between the individual list items.
• To get the content from <input> or <textarea> elements, use .val() method
Getting at Content
The .Append() method lets you add content to the page.
c07/js/get-html-fragment.js
• The selector returns the <ul> element.
• The •html() method gets all the HTML
inside it (the four <li> elements).
• This is then appended to the end of
the selection, in this case after the
existing <li>elements.
Getting at Content
c07/js/get-text-fragment.js
• The selector returns the <ul> element.
• The .text() method gets the text from
all of the <ul>element's children.
• This is then appended to the end of
the selection, in this case after the
existing <ul>element.
Getting at Content
c07/js/get-html-node.js
c07/js/get-text-node.js
• Changing the events with the new CSS rules is popular for web page interactive.
1. The first finds the third list item (id attribute with a value of three)
and removes hot from the class attribute on that element.
2. The second selects all <li> elements whose class attribute has a value of hot. It
adds a new class name called favorite.
3. The third selects the <ul> element and adds an id attribute, giving it a value of
group (trigger a CSS rule that will add a margin and border to the <ul> element).
Getting & Setting CSS Properties
• The .css() method lets you retrieve and set the values of CSS properties.
• To get the value of a CSS property, you indicate which property you
want to retrieve in parentheses.
• To set the values of a CSS property, you specify the property name as
the first argument in the parentheses, then a comma, followed by its
value as the second argument.
HOW TO GET A CSS PROPERTY
var backgroundColor = $( 'li' ).css( 'background-color' );
• It stores the background color of the first list item in a variable called
backgroundColor as RGB value.
Getting & Setting CSS Properties
HOW TO SET A CSS PROPERTY
$('li').css( 'background-color', ‘#272727' );
• It sets the background color of all list items. The CSS property and its
value are separated using a comma.
$('li').css( 'padding-left', '+=20');
• With dimensions in pixels, you can increase and decrease the values
using the+= and-= operators.
SETTING MULTIPLE PROPERTIES
• You can set multiple properties using object literal notation:
• Properties and values are placed in curly braces
• A colon is used to separate property names from their values
• A comma separates each pair (not one after the last pair)
$('li').css({ 'background-color' : '#272727', 'font-family' : 'Courier'});
Changing CSS Rules
• Check what the background color of the first list item is when the page
loads and then writes it after the list.
• Next, it updates several CSS properties in all list items using the same
.css() method with object literal notation. c07/js/css.js
Working with Each Element in a Selection
• jQuery allows you to recreate the functionality of a loop on a selection of
elements, using the •each() method. The parameter of the •each ()
method is a function, anonymous function or a named function.
Working with Each Element in a Selection
c07/js/each.js
Event Methods
• The •on() method is used to handle all events whereas jQuery handles
all of the cross-browser issues.
• Use a selector to create a jQuery selection.
• Use .on() to indicate which event to respond to in the selection.
• jQuery used separate methods for each event such as .click() and
.focus() but now should only use the .on() method in v 1.7 or obove.
1. The jQuery selection contains all of the <li>elements.
2. The .on() method is used to handle events with two parameters.
3. The first parameter is the event to respond to (the click event).
4. The second parameter runs when that event occurs on any element
in the matched set. This could be a named or an anonymous function.
Event Methods
JQUERY EVENTS
• Some of the most popular events that •on() deals with are listed:
• jQuery added ready to fire when the page is ready to be worked with *
Events c07/js/events.js
Events
The Event Object
• Every event handling function receives an event object as the letter e.
• The event object (e) is named in the parentheses when the event occurs.
• It has methods and properties like the JavaScript.
1. Give event object a parameter name.
2. Use that name in the function to
reference the event object.
3. Access the properties and methods of
the object using dot notation (member
operator).
The Event Object
The Event Object
c07/js/event-object.js
1. The event(s) you want to respond to. 'focus click' will work on both focus and
click if using a space separated list of event names.
2. The event happening on a subset of the elements in the initial jQuery selection.
3. Extra information to function when the event is triggered with event object (e).
4. Function runs when the specified events occur
5. The function is automatically passed the event object as a parameter.
• Older jQuery scripts may use the .delegate() method. jQuery 1.7 •on() is the
preferred approach to delegation.
Delegating Events c07/js/event-delegation.js