SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
jQuery
Introduction to using jQuery
ugo.rinaldi@gmail.com
jQuery
• jQuery is a lightweight, "write less, do more", JavaScript
library.
• The purpose of jQuery is to make it much easier to use
JavaScript on your website.
• jQuery takes a lot of common tasks that require many
lines of JavaScript code to accomplish, and wraps them
into methods that you can call with a single line of code.
• jQuery also simplifies a lot of the complicated things
from JavaScript, like AJAX calls and DOM manipulation.
Features
The jQuery library contains the following features:
 HTML/DOM manipulation
 CSS manipulation
 HTML event methods
 Effects and animations
 AJAX
 Utilities
 jQuery will run exactly the same in all major browsers,
including Internet Explorer 6!
There are several ways to start using jQuery on your web
site. You can:
 Download the jQuery library from jQuery.com
 Include jQuery from a CDN, like Google
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.
min.js"> </script>
</head>
Adding jQuery
 The jQuery syntax is tailor made for selecting HTML elements and
performing some action on the element(s).
 Basic syntax is: $(selector).action()
 A $ sign to define/access jQuery
 A (selector) to "query (or find)" HTML elements
 A jQuery action() to be performed on the element(s)
 Examples:
 $(this).hide() - hides the current element.
 $("p").hide() - hides all <p> elements.
 $(".test").hide() - hides all elements with class="test".
 $("#test").hide() - hides the element with id="test".
jQuery Syntax
 $("*") Selects all elements
 $(this) Selects the current HTML element
 $("p.intro") Selects all <p> elements with class="intro"
 $("p:first") Selects the first <p> element
 $("ul li:first") Selects the first <li> element of the first <ul>
 $("ul li:first-child") Selects the first <li> element of every <ul>
 $("[href]") Selects all elements with an href attribute
 $("a[target='_blank']") Selects all <a> elements with a target attribute value equal to
"_blank"
 $("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal
to "_blank"
 $(":button") Selects all <button> elements and <input> elements of type="button"
 $("tr:even") Selects all even <tr> elements
 $("tr:odd") Selects all odd <tr> elements
jQuery selectors like CSS syntax
 All the different visitor's actions that a web page can
respond to, are called events.
 An event represents the precise moment when
something happens.
Examples:
 moving a mouse over an element
 selecting a radio button
 clicking on an element
Events
• $(document).ready(function(){
// Insert one of the following event functions
});
• $("p").click(function(){ …});
• $("p").dblclick(function(){ …});
• $("p").mouseenter(function(){ //when mouse pointer enters});
• $("p").mouseleave(function(){//when mouse pointer leaves});
• $("p").mousedown(function(){//when click + over});
• $("p").mouseup(function(){//when release + over});
●
$("#p1").hover(
function(){ alert("You entered p1!");},
function(){ alert("Bye! You now leave p1!"); }
);
Examples
 $("p").on({
focus: function(){
$(this).css("background-color", "lightgray");
},
blur: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});
Attach event handlers to elements
Effects hide, show, fade
 $("#hide").click(function(){
$("p").hide(); // $("p").hide(1000);
});
 $("p").show();
 $("p").toggle();
 $(“p”).fadeIn(”slow”,callback function);
 $(“p”).fadeOut(”fast”,callback function);
 $(“p”).fadeToggle(1500,callback function);
 $(“p”).fadeTo(”fast”,0.5,callback function);
 With jQuery you can create a sliding effect on elements.
 jQuery has the following slide methods:
 slideDown()
 slideUp()
 slideToggle()
 Syntax:
 $(selector).method(speed,callback);
jQuery Sliding Methods
 $("button").click(function(){
$("div").animate({ left: '250px‘, opacity: '0.5‘, height: '150px‘, width: ‘+=5px’ });
});
 $("button").click(function(){
var div = $("div");
div.animate({height: '300px', opacity: '0.4'}, "slow");
div.animate({width: '300px', opacity: '0.8'}, "slow");
div.animate({height: '100px', opacity: '0.4'}, "slow");
div.animate({width: '100px', opacity: '0.8'}, "slow");
});
 $("div").animate({
height: 'toggle’ });
 $("#stop").click(function(){
$("#panel").stop(); // interrompe un’animazione
});
Animation
 A callback function is executed after the current effect is 100%
finished.
$("button").click(function(){
$("p").hide("slow", function(){
alert("The paragraph is now hidden");
});
});
 $("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
Callback function
 $("#p1").css("color",
"red").slideUp(2000).slideDown(2000);
 Si può scrivere anche
$("#p1").css("color", "red")
.slideUp(2000)
.slideDown(2000);
Method chaining
Simple, but useful, jQuery methods for DOM
manipulation/examine are:
 text() - Sets or returns the text content of
selected elements
 html() - Sets or returns the content of selected
elements (including HTML markup)
 val() - Sets or returns the value of form fields
 attr() – Gets attribute value
DOM manipulation
 // per leggere
 $("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
 // per scrivere; unico metodo che richiede 2 parametri
 $("button").click(function(){
$("#w3s").attr("href", "http://www.w3schools.com/jquery");
});
 $("button").click(function(){
$("#w3s").attr({
"href" : "http://www.w3schools.com/jquery",
"title" : "W3Schools jQuery Tutorial"
});
});
Esempi
 $("p").css("background-color");
 $("p").css("background-color", "yellow");
 $("p").css({"background-color": "yellow", "font-
size": "200%"});
css() Method
Dimensions
 $("button").click(function(){
var txt = "";
txt += "Width: " + $("#div1").width() + "</br>";
txt += "Height: " + $("#div1").height();
$("#div1").html(txt);
});
 $("button").click(function(){
var txt = "";
txt += "Document width/height: " + $(document).width();
txt += "x" + $(document).height() + "n";
txt += "Window width/height: " + $(window).width();
txt += "x" + $(window).height();
alert(txt);
});
 $("button").click(function(){
$("#div1").width(500).height(500);
});// per settare insieme widht & height
…
 AJAX = Asynchronous JavaScript and XML.
 In short: AJAX is about loading data in the
background and display it on the webpage,
without reloading the whole page.
Ajax
load()
 Syntax:
$(selector).load(URL,data,callback(response,status,xhr));
 $(“#btn1”).click(function(){$
("#div1").load("demo.txt"});
 Esempio
AJAX get() and post() Methods
 The jQuery get() and post() methods are used to request data from the server with an HTTP request.
 Syntax:
$.get(URL,callback(data, status));
$.post(URL,data,callback);
Esempi:
$("#btn1").click(function(){
$.get("demo_test.asp", function(data, status){
alert("Data: " + data + "nStatus: " + status);
});
});
$("button").click(function(){
$.post("demo_test_post.asp",
{ name: "Donald Duck",
city: "Duckburg" },
function(data, status){
alert("Data: " + data + "nStatus: " + status); });
});
Esempi e sitografia
Qui puoi trovare esempi utili
Qui puoi trovare l'intero tutorial ed una Refer
ences Guide
http://www.w3schools.com

Contenu connexe

Tendances

Tendances (20)

Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
J Query Public
J Query PublicJ Query Public
J Query Public
 
A Short Introduction To jQuery
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuery
 
Backbone - TDC 2011 Floripa
Backbone - TDC 2011 FloripaBackbone - TDC 2011 Floripa
Backbone - TDC 2011 Floripa
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 
jQuery
jQueryjQuery
jQuery
 
jQuery
jQueryjQuery
jQuery
 
Jquery introduction
Jquery introductionJquery introduction
Jquery introduction
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
jQuery
jQueryjQuery
jQuery
 
J query training
J query trainingJ query training
J query training
 
Iterators & generators: practical uses in memory management
Iterators & generators: practical uses in memory managementIterators & generators: practical uses in memory management
Iterators & generators: practical uses in memory management
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
jQuery - Chapter 5 - Ajax
jQuery - Chapter 5 -  AjaxjQuery - Chapter 5 -  Ajax
jQuery - Chapter 5 - Ajax
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
 
JavaScript and jQuery Basics
JavaScript and jQuery BasicsJavaScript and jQuery Basics
JavaScript and jQuery Basics
 
jQuery
jQueryjQuery
jQuery
 

Similaire à Introduzione JQuery

presentation_jquery_ppt.pptx
presentation_jquery_ppt.pptxpresentation_jquery_ppt.pptx
presentation_jquery_ppt.pptx
azz71
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
Laila Buncab
 

Similaire à Introduzione JQuery (20)

Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
jQuery
jQueryjQuery
jQuery
 
JQuery
JQueryJQuery
JQuery
 
jQuery Presentasion
jQuery PresentasionjQuery Presentasion
jQuery Presentasion
 
JQuery_and_Ajax.pptx
JQuery_and_Ajax.pptxJQuery_and_Ajax.pptx
JQuery_and_Ajax.pptx
 
J query
J queryJ query
J query
 
Unit3.pptx
Unit3.pptxUnit3.pptx
Unit3.pptx
 
A Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NETA Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NET
 
presentation_jquery_ppt.pptx
presentation_jquery_ppt.pptxpresentation_jquery_ppt.pptx
presentation_jquery_ppt.pptx
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 
Introduction to jQuery - The basics
Introduction to jQuery - The basicsIntroduction to jQuery - The basics
Introduction to jQuery - The basics
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NET
 
jQuery besic
jQuery besicjQuery besic
jQuery besic
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
How to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQueryHow to increase Performance of Web Application using JQuery
How to increase Performance of Web Application using JQuery
 
Jquery 3
Jquery 3Jquery 3
Jquery 3
 
JQuery Overview
JQuery OverviewJQuery Overview
JQuery Overview
 
Jquery
JqueryJquery
Jquery
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
 

Plus de orestJump (8)

Python - Primi passi
Python - Primi passi Python - Primi passi
Python - Primi passi
 
Tecnologie informatiche
Tecnologie informaticheTecnologie informatiche
Tecnologie informatiche
 
Php mysql3
Php mysql3Php mysql3
Php mysql3
 
Introduzione ai Sistemi Operativi
Introduzione ai Sistemi OperativiIntroduzione ai Sistemi Operativi
Introduzione ai Sistemi Operativi
 
Introduzione al livello di rete e Dijkstra algorithm
Introduzione al livello di rete e Dijkstra algorithmIntroduzione al livello di rete e Dijkstra algorithm
Introduzione al livello di rete e Dijkstra algorithm
 
Flow chart
Flow chartFlow chart
Flow chart
 
Php mysql e cms
Php mysql e cmsPhp mysql e cms
Php mysql e cms
 
Html e CSS ipertesti e siti web 4.5
Html e CSS   ipertesti e siti web 4.5Html e CSS   ipertesti e siti web 4.5
Html e CSS ipertesti e siti web 4.5
 

Dernier

Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
nilamkumrai
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
ydyuyu
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
nirzagarg
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men 🔝mehsana🔝 Escorts...
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men  🔝mehsana🔝   Escorts...➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men  🔝mehsana🔝   Escorts...
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men 🔝mehsana🔝 Escorts...
nirzagarg
 

Dernier (20)

All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
 
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
 
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Salem Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
 
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men 🔝mehsana🔝 Escorts...
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men  🔝mehsana🔝   Escorts...➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men  🔝mehsana🔝   Escorts...
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men 🔝mehsana🔝 Escorts...
 

Introduzione JQuery

  • 1. jQuery Introduction to using jQuery ugo.rinaldi@gmail.com
  • 2. jQuery • jQuery is a lightweight, "write less, do more", JavaScript library. • The purpose of jQuery is to make it much easier to use JavaScript on your website. • jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code. • jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.
  • 3. Features The jQuery library contains the following features:  HTML/DOM manipulation  CSS manipulation  HTML event methods  Effects and animations  AJAX  Utilities  jQuery will run exactly the same in all major browsers, including Internet Explorer 6!
  • 4. There are several ways to start using jQuery on your web site. You can:  Download the jQuery library from jQuery.com  Include jQuery from a CDN, like Google <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery. min.js"> </script> </head> Adding jQuery
  • 5.  The jQuery syntax is tailor made for selecting HTML elements and performing some action on the element(s).  Basic syntax is: $(selector).action()  A $ sign to define/access jQuery  A (selector) to "query (or find)" HTML elements  A jQuery action() to be performed on the element(s)  Examples:  $(this).hide() - hides the current element.  $("p").hide() - hides all <p> elements.  $(".test").hide() - hides all elements with class="test".  $("#test").hide() - hides the element with id="test". jQuery Syntax
  • 6.  $("*") Selects all elements  $(this) Selects the current HTML element  $("p.intro") Selects all <p> elements with class="intro"  $("p:first") Selects the first <p> element  $("ul li:first") Selects the first <li> element of the first <ul>  $("ul li:first-child") Selects the first <li> element of every <ul>  $("[href]") Selects all elements with an href attribute  $("a[target='_blank']") Selects all <a> elements with a target attribute value equal to "_blank"  $("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal to "_blank"  $(":button") Selects all <button> elements and <input> elements of type="button"  $("tr:even") Selects all even <tr> elements  $("tr:odd") Selects all odd <tr> elements jQuery selectors like CSS syntax
  • 7.  All the different visitor's actions that a web page can respond to, are called events.  An event represents the precise moment when something happens. Examples:  moving a mouse over an element  selecting a radio button  clicking on an element Events
  • 8. • $(document).ready(function(){ // Insert one of the following event functions }); • $("p").click(function(){ …}); • $("p").dblclick(function(){ …}); • $("p").mouseenter(function(){ //when mouse pointer enters}); • $("p").mouseleave(function(){//when mouse pointer leaves}); • $("p").mousedown(function(){//when click + over}); • $("p").mouseup(function(){//when release + over}); ● $("#p1").hover( function(){ alert("You entered p1!");}, function(){ alert("Bye! You now leave p1!"); } ); Examples
  • 9.  $("p").on({ focus: function(){ $(this).css("background-color", "lightgray"); }, blur: function(){ $(this).css("background-color", "lightblue"); }, click: function(){ $(this).css("background-color", "yellow"); } }); Attach event handlers to elements
  • 10. Effects hide, show, fade  $("#hide").click(function(){ $("p").hide(); // $("p").hide(1000); });  $("p").show();  $("p").toggle();  $(“p”).fadeIn(”slow”,callback function);  $(“p”).fadeOut(”fast”,callback function);  $(“p”).fadeToggle(1500,callback function);  $(“p”).fadeTo(”fast”,0.5,callback function);
  • 11.  With jQuery you can create a sliding effect on elements.  jQuery has the following slide methods:  slideDown()  slideUp()  slideToggle()  Syntax:  $(selector).method(speed,callback); jQuery Sliding Methods
  • 12.  $("button").click(function(){ $("div").animate({ left: '250px‘, opacity: '0.5‘, height: '150px‘, width: ‘+=5px’ }); });  $("button").click(function(){ var div = $("div"); div.animate({height: '300px', opacity: '0.4'}, "slow"); div.animate({width: '300px', opacity: '0.8'}, "slow"); div.animate({height: '100px', opacity: '0.4'}, "slow"); div.animate({width: '100px', opacity: '0.8'}, "slow"); });  $("div").animate({ height: 'toggle’ });  $("#stop").click(function(){ $("#panel").stop(); // interrompe un’animazione }); Animation
  • 13.  A callback function is executed after the current effect is 100% finished. $("button").click(function(){ $("p").hide("slow", function(){ alert("The paragraph is now hidden"); }); });  $("button").click(function(){ $("p").hide(1000); alert("The paragraph is now hidden"); }); Callback function
  • 14.  $("#p1").css("color", "red").slideUp(2000).slideDown(2000);  Si può scrivere anche $("#p1").css("color", "red") .slideUp(2000) .slideDown(2000); Method chaining
  • 15. Simple, but useful, jQuery methods for DOM manipulation/examine are:  text() - Sets or returns the text content of selected elements  html() - Sets or returns the content of selected elements (including HTML markup)  val() - Sets or returns the value of form fields  attr() – Gets attribute value DOM manipulation
  • 16.  // per leggere  $("#btn1").click(function(){ alert("Text: " + $("#test").text()); });  // per scrivere; unico metodo che richiede 2 parametri  $("button").click(function(){ $("#w3s").attr("href", "http://www.w3schools.com/jquery"); });  $("button").click(function(){ $("#w3s").attr({ "href" : "http://www.w3schools.com/jquery", "title" : "W3Schools jQuery Tutorial" }); }); Esempi
  • 17.  $("p").css("background-color");  $("p").css("background-color", "yellow");  $("p").css({"background-color": "yellow", "font- size": "200%"}); css() Method
  • 19.  $("button").click(function(){ var txt = ""; txt += "Width: " + $("#div1").width() + "</br>"; txt += "Height: " + $("#div1").height(); $("#div1").html(txt); });  $("button").click(function(){ var txt = ""; txt += "Document width/height: " + $(document).width(); txt += "x" + $(document).height() + "n"; txt += "Window width/height: " + $(window).width(); txt += "x" + $(window).height(); alert(txt); });  $("button").click(function(){ $("#div1").width(500).height(500); });// per settare insieme widht & height …
  • 20.  AJAX = Asynchronous JavaScript and XML.  In short: AJAX is about loading data in the background and display it on the webpage, without reloading the whole page. Ajax
  • 22. AJAX get() and post() Methods  The jQuery get() and post() methods are used to request data from the server with an HTTP request.  Syntax: $.get(URL,callback(data, status)); $.post(URL,data,callback); Esempi: $("#btn1").click(function(){ $.get("demo_test.asp", function(data, status){ alert("Data: " + data + "nStatus: " + status); }); }); $("button").click(function(){ $.post("demo_test_post.asp", { name: "Donald Duck", city: "Duckburg" }, function(data, status){ alert("Data: " + data + "nStatus: " + status); }); });
  • 23. Esempi e sitografia Qui puoi trovare esempi utili Qui puoi trovare l'intero tutorial ed una Refer ences Guide http://www.w3schools.com