SlideShare ist ein Scribd-Unternehmen logo
1 von 12
What is jQuery?
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. With a combination of versatility and
extensibility, jQuery has changed the way that millions of people write
JavaScript.
Who's Using jQuery?
How jQuery Works
jQuery: The Basics
This is a basic tutorial, designed to help you get started using jQuery. If you don't have a test page setup yet, start by creating
the following HTML page:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo</title>
</head>
<body>
<a href="http://jquery.com/">jQuery</a>
<script src="jquery.js"></script>
<script>
// Your code goes here.
</script>
</body>
</html>

The src attribute in the <script> element must point to a copy of jQuery. Download a copy of jQuery from
the Downloading jQuery page and store t jquery.js file in the same directory as your HTML file.
Launching Code on Document Ready
Unfortunately, the code doesn't run until all images are finished downloading, including banner ads. To run code as soon as the
document is ready to be manipulated, jQuery has a statement known as the ready event:
1
2
3
4
5

window.onload = function() {
alert( "welcome" );
}

To ensure that their code runs after the browser finishes loading the document, many JavaScript programmers wrap their code in an
onload function:
1
2
3
4
5

$( document ).ready(function() {
// Your code here.
});

For example, inside the ready event, you can add a click handler to the link:
1
2
3
4
5
6
7
8
9

$( document ).ready(function() {
$( "a" ).click(function( event ) {
alert( "Thanks for visiting!" );
});
});
Save your HTML file and reload the test page in your browser. Clicking the link should now first display an alert pop-up, then
continue with the default behavior of navigating to http://jquery.com.
For click and most other events, you can prevent the default behavior by calling event.preventDefault() in the event handler:

1
2
3
4
5
6
7
8
9
1
0
1
1

$( document ).ready(function() {
$( "a" ).click(function( event ) {
alert( "As you can see, the link no longer took you to jquery.com" );
event.preventDefault();
});
});
Adding and Removing an HTML Class
Important: You must place the remaining jQuery examples inside the ready event so that your code executes when the document
is ready to be worked on.
Another common task is adding or removing a class.

First, add some style information into the <head> of the document, like this:
1
2
3
4
5

<style>
a.test {
font-weight: bold;
}
</style>

Next, add the .addClass() call to the script:
1

$( "a" ).addClass( "test" );

All <a> elements are now bold.
To remove an existing class, use .removeClass():
1

$( "a" ).removeClass( "test" );
Special Effects
jQuery also provides some handy effects to help you make your web sites
stand out. For example, if you create a click handler of:

1
2
3
4
5
6
7

$( "a" ).click(function( event ) {
event.preventDefault();
$( this ).hide( "slow" );
});

Then the link slowly disappears when clicked.
Callbacks and Functions
Unlike many other programming languages, JavaScript enables you to
freely pass functions around to be executed at a later time. A callback is a
function that is passed as an argument to another function and is executed
after its parent function has completed. Callbacks are special because they
patiently wait to execute until their parent finishes. Meanwhile, the browser
can be executing other functions or doing all sorts of other work.
To use callbacks, it is important to know how to pass them into their parent
function.
Callback without Arguments
If a callback has no arguments, you can pass it in like this:

1

$.get( "myhtmlpage.html", myCallBack );

When $.get() finishes getting the page myhtmlpage.html, it executes the myCallBack() function.
•Note: The second parameter here is simply the function name (but not as a string, and without parentheses).
Callback with Arguments
Executing callbacks with arguments can be tricky.
Wrong
This code example will not work:
1

$.get( "myhtmlpage.html", myCallBack( param1, param2 ) );

The reason this fails is that the code executes myCallBack( param1, param2 ) immediately and then passes myCallBack()'s return
value as the second parameter to $.get(). We actually want to pass the function myCallBack(), notmyCallBack( param1, param2 )'s
return value (which might or might not be a function). So, how to pass in myCallBack() andinclude its arguments?

Right
To defer executing myCallBack() with its parameters, you can use an anonymous function as a wrapper. Note the use
offunction() {. The anonymous function does exactly one thing: calls myCallBack(), with the values of param1 and param2.
1
2
3
4
5

$.get( "myhtmlpage.html", function() {
myCallBack( param1, param2 );
});

When $.get() finishes getting the page myhtmlpage.html, it executes the anonymous function, which executes
myCallBack( param1, param2 ).
• Thank you ..

Weitere ähnliche Inhalte

Was ist angesagt?

Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Christian Lilley
 

Was ist angesagt? (20)

React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
Using schemas in parsing xml part 2
Using schemas in parsing xml part 2Using schemas in parsing xml part 2
Using schemas in parsing xml part 2
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
Gems Of Selenium
Gems Of SeleniumGems Of Selenium
Gems Of Selenium
 
My Test Automation Journey
My Test Automation JourneyMy Test Automation Journey
My Test Automation Journey
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
Jsp intro
Jsp introJsp intro
Jsp intro
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
MeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenmentMeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenment
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and Redux
 
Switch to React.js from AngularJS developer
Switch to React.js from AngularJS developerSwitch to React.js from AngularJS developer
Switch to React.js from AngularJS developer
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
Introduction to angular js for .net developers
Introduction to angular js  for .net developersIntroduction to angular js  for .net developers
Introduction to angular js for .net developers
 
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
 
1 ppt-ajax with-j_query
1 ppt-ajax with-j_query1 ppt-ajax with-j_query
1 ppt-ajax with-j_query
 
Javascript void
Javascript voidJavascript void
Javascript void
 
Redux vs Alt
Redux vs AltRedux vs Alt
Redux vs Alt
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 

Andere mochten auch (9)

NLADA 2008: Language Access and Technology : Reaching Limited English Profici...
NLADA 2008: Language Access and Technology: Reaching Limited English Profici...NLADA 2008: Language Access and Technology: Reaching Limited English Profici...
NLADA 2008: Language Access and Technology : Reaching Limited English Profici...
 
Css selectors
Css selectorsCss selectors
Css selectors
 
Banosdelmundo
BanosdelmundoBanosdelmundo
Banosdelmundo
 
[Challenge:Future] IDealMaribor
[Challenge:Future] IDealMaribor[Challenge:Future] IDealMaribor
[Challenge:Future] IDealMaribor
 
JQuery Basics
JQuery BasicsJQuery Basics
JQuery Basics
 
Introduction To Parallel Computing
Introduction To Parallel ComputingIntroduction To Parallel Computing
Introduction To Parallel Computing
 
Open Innovation
Open InnovationOpen Innovation
Open Innovation
 
Mary Bermeo
Mary BermeoMary Bermeo
Mary Bermeo
 
Inteligencia Colectiva
Inteligencia ColectivaInteligencia Colectiva
Inteligencia Colectiva
 

Ähnlich wie jQuery basics

Ähnlich wie jQuery basics (20)

jQuery
jQueryjQuery
jQuery
 
JavaScript Libraries
JavaScript LibrariesJavaScript Libraries
JavaScript Libraries
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
 
JavaScript: DOM and jQuery
JavaScript: DOM and jQueryJavaScript: DOM and jQuery
JavaScript: DOM and jQuery
 
jQuery for web development
jQuery for web developmentjQuery for web development
jQuery for web development
 
react-en.pdf
react-en.pdfreact-en.pdf
react-en.pdf
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
 
Jquery tutorial-beginners
Jquery tutorial-beginnersJquery tutorial-beginners
Jquery tutorial-beginners
 
Web Components v1
Web Components v1Web Components v1
Web Components v1
 
jQuery and_drupal
jQuery and_drupaljQuery and_drupal
jQuery and_drupal
 
Modern Web Technologies
Modern Web TechnologiesModern Web Technologies
Modern Web Technologies
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
 
Think jQuery
Think jQueryThink jQuery
Think jQuery
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
Jqueryppt (1)
Jqueryppt (1)Jqueryppt (1)
Jqueryppt (1)
 
jQuery - Chapter 3 - Effects
jQuery - Chapter 3 - Effects  jQuery - Chapter 3 - Effects
jQuery - Chapter 3 - Effects
 

Kürzlich hochgeladen

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

jQuery basics

  • 1.
  • 2. What is jQuery? 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. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.
  • 4. How jQuery Works jQuery: The Basics This is a basic tutorial, designed to help you get started using jQuery. If you don't have a test page setup yet, start by creating the following HTML page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Demo</title> </head> <body> <a href="http://jquery.com/">jQuery</a> <script src="jquery.js"></script> <script> // Your code goes here. </script> </body> </html> The src attribute in the <script> element must point to a copy of jQuery. Download a copy of jQuery from the Downloading jQuery page and store t jquery.js file in the same directory as your HTML file.
  • 5. Launching Code on Document Ready Unfortunately, the code doesn't run until all images are finished downloading, including banner ads. To run code as soon as the document is ready to be manipulated, jQuery has a statement known as the ready event: 1 2 3 4 5 window.onload = function() { alert( "welcome" ); } To ensure that their code runs after the browser finishes loading the document, many JavaScript programmers wrap their code in an onload function: 1 2 3 4 5 $( document ).ready(function() { // Your code here. }); For example, inside the ready event, you can add a click handler to the link: 1 2 3 4 5 6 7 8 9 $( document ).ready(function() { $( "a" ).click(function( event ) { alert( "Thanks for visiting!" ); }); });
  • 6. Save your HTML file and reload the test page in your browser. Clicking the link should now first display an alert pop-up, then continue with the default behavior of navigating to http://jquery.com. For click and most other events, you can prevent the default behavior by calling event.preventDefault() in the event handler: 1 2 3 4 5 6 7 8 9 1 0 1 1 $( document ).ready(function() { $( "a" ).click(function( event ) { alert( "As you can see, the link no longer took you to jquery.com" ); event.preventDefault(); }); });
  • 7. Adding and Removing an HTML Class Important: You must place the remaining jQuery examples inside the ready event so that your code executes when the document is ready to be worked on. Another common task is adding or removing a class. First, add some style information into the <head> of the document, like this: 1 2 3 4 5 <style> a.test { font-weight: bold; } </style> Next, add the .addClass() call to the script: 1 $( "a" ).addClass( "test" ); All <a> elements are now bold. To remove an existing class, use .removeClass(): 1 $( "a" ).removeClass( "test" );
  • 8. Special Effects jQuery also provides some handy effects to help you make your web sites stand out. For example, if you create a click handler of: 1 2 3 4 5 6 7 $( "a" ).click(function( event ) { event.preventDefault(); $( this ).hide( "slow" ); }); Then the link slowly disappears when clicked.
  • 9. Callbacks and Functions Unlike many other programming languages, JavaScript enables you to freely pass functions around to be executed at a later time. A callback is a function that is passed as an argument to another function and is executed after its parent function has completed. Callbacks are special because they patiently wait to execute until their parent finishes. Meanwhile, the browser can be executing other functions or doing all sorts of other work. To use callbacks, it is important to know how to pass them into their parent function.
  • 10. Callback without Arguments If a callback has no arguments, you can pass it in like this: 1 $.get( "myhtmlpage.html", myCallBack ); When $.get() finishes getting the page myhtmlpage.html, it executes the myCallBack() function. •Note: The second parameter here is simply the function name (but not as a string, and without parentheses).
  • 11. Callback with Arguments Executing callbacks with arguments can be tricky. Wrong This code example will not work: 1 $.get( "myhtmlpage.html", myCallBack( param1, param2 ) ); The reason this fails is that the code executes myCallBack( param1, param2 ) immediately and then passes myCallBack()'s return value as the second parameter to $.get(). We actually want to pass the function myCallBack(), notmyCallBack( param1, param2 )'s return value (which might or might not be a function). So, how to pass in myCallBack() andinclude its arguments? Right To defer executing myCallBack() with its parameters, you can use an anonymous function as a wrapper. Note the use offunction() {. The anonymous function does exactly one thing: calls myCallBack(), with the values of param1 and param2. 1 2 3 4 5 $.get( "myhtmlpage.html", function() { myCallBack( param1, param2 ); }); When $.get() finishes getting the page myhtmlpage.html, it executes the anonymous function, which executes myCallBack( param1, param2 ).
  • 12. • Thank you ..