Download presentation
Presentation is loading. Please wait.
Published byJob Anthony Modified over 8 years ago
1
jQuery is a fast, small, and feature-rich javascript library. It makes things like HTML document traversal and manipulation, event handling, animation, and ajax much simpler with an easy-to-use API that works across a multitude of browsers.
2
WHAT CAN IT DO? Change content based on user actions Toggle CSS classes to highlight an element Makes it easy to: Find elements in the DOM Change content Listen to what the user does Animation content on the page Talk over network to fetch new content
3
HOW TO GET THE LIBRARY: METHOD 1 Download it from here: http://jquery.com/http://jquery.com/ Site Features lots of API/documentation Load into your page: Make sure it is first script included if you also include your own js file that uses it
4
JQUERY IMPROVES OUR CODE EXAMPLE: OBTRUSIVE JAVASCRIPT
5
OUR FORM VALIDATION EXAMPLE Separate most of the JavaScript in its own.js file Styles have been separated out into a style sheet BUT….we still have calls to the functions appearing within our html in the form elements
6
GOAL = COMPLETE SEPARATION Idea is called Unobtrusive JavaScript Considers any JS embedded in the to be incorrect This includes attributes of HTML elements (such as onblur ) Using the jQuery library, we can achieve complete separation of style, behavior, and structure
7
HOW TO GET THE LIBRARY: OPTION 2 Quicker/Easier Use a Google-hosted library by grabbing the snippet from here: https://developers.google.com/sp eed/libraries/#jquery Add snippet code in your page above your own js
8
JQUERY FUNCTION In General – format is: jQuery( ); Shorthand Equivalent: $( );
9
DON’T WANT TO DO ANYTHING UNTIL DOM FINISHES LOADING Determine using ready function The DOM Event triggers once page fully loaded
10
FINDING NODES (ELEMENTS) IN THE DOM Just need to use their CSS selectors To grab this and this, just use regular CSS selector that would get them
11
TO ACCESS TEXT INSIDE AN ELEMENT.text() Equivalent to JS.innerHTML.text() retrieves current contents.text(“new value”) sets to new value
12
EXAMPLE HTML jQuery Result
13
FIND BASED ON ID OR CLASS ID’s: $(“#whateverId”) Classes: $(“.whateverClass”)
14
MORE SUB-SELECTION – STILL JUST BASED ON WHAT CSS WOULD LOOK LIKE $("p a") Retrieves any links nested inside a element $("div.exampleClass") Retrieves any element with class exampleClass
15
EXAMPLE HTML
16
EXAMPLE - CONTINUED CSS jQuery Result EVERY li descendent of #favs is selected
17
SELECTING MULTIPLE ELEMENTS Again, just like CSS List them with comma in between Example: $(“.movies, #foods”) Selects elements with class movies or id foods
18
PSEUDO-CLASSES li:first only selects 1 st list item li:last only selects last li li:odd only selects odd li’s li:even only selects even li’s
19
PERFORM AN OPERATION Once set is retrieved, can perform operations on it Basic Syntax: $(selector).action( ) Example: $("div.exampleClass").hide(); This will hide any element with class exampleClass
20
CAN DAISY-CHAIN OPERATIONS tack various transformations together with additional periods Example: $("div.ex").hide().addClass("removed"); The code 1 st hides any with class ex and then adds an additional class to all of the these same elements called removed
21
RESPOND TO AN EVENT $(document).ready(function(){ $("selector").event(function(){ /*do whatever in response */ });
22
EXAMPLE $(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); }); This is a heading This is a paragraph. This is another paragraph. Click me
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.