SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
Practice jQuery




   Inbal Geffen
Add jQuery to your project

   ● Download JQuery from the jQuery website
     and use this code : <script src=‘jquery.js’></script>

         OR

   ● Link to a CDN hosted jQuery
      and use a code like this :
  <script src=‘http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js’>
  </script>




Inbal Geffen
$("#div1") == jQuery("#div1")

  <script src="jquery.js" type="text/javascript"></script>

  <script type="text/javascript">
  function hellojQuery() {
  $("#div1").html("Hello jQuery");
  }
  </script>

    ●    The $ sign tells us to choose an element from our code
    ●    We can choose elements using all the known css selectors
    ●    $("#div1") - will choose all the elements in the page with the id div1
    ●    .html - is like using innerHTML on all the selected elements


  Example

Inbal Geffen
Select Elements - CSS selectors

   ● $(“#div1”) - select all elements with "div1" id

   ● $(“div”) - select all div elements

   ● $(“.classic”) - select all elements with classic class

   ● $(“div.classic”) - select all div elements with classic class

   ● $(“div:last”) - select the last div

   ● $(“div:even”) - select all divs in even places

  and all the rest..


Inbal Geffen
Select Elements - more selectors

   ● $(“:button”) - select all buttons

   ● $(“:contains(hello)”) - select all elements that contain the text "hello"

   ● $(“:hidden”) - select all hidden elements

   ● $(“:visible”) - select all visible elements

   ● $(“div:not(.classic)”) - select all elements that are not from classic class




Inbal Geffen
Select Elements - DOM tree

  <p id="p1">
  This is a first link: <a href="page1.html">Page1</a><br />
  This is a second link: <a href="page2.html">Page2</a><br />
  This is a third link: <a href="page3.html">Page3</a><br />
  </p>



   ● $(“p”).parent() - select the parent p

   ● $(“p”).children("a") - select all the a elements in the p

   ● $(“div”).siblings() - select the parent div

   ● $(“div”).next('div') - select the next div in the DOM

   ● $(“div”).prev('p') - select the previous p in the DOM


Inbal Geffen
What can we do with it?

  We used the $ to select elements from our page -
  what can we do with these elements?

   ● $(“div”)[0] - will return the first div element in the page

   ● $(“div”).size() - will return how many divs are in the page



  What else?
   ● Change the element attributes
   ● Change the element content
   ● Dynamically add elements to the page

Inbal Geffen
Change element content


   ● $(“div”).text("I am a div") - will change only the text in the div

   ● $(“div”).html(“I am a div”) - change the innerHTML of all the divs
  //This is instead of going in a loop over all the divs

  //We can iterate over each div using the each method
  function showDivNumber()
  {
       $("div").each(function (index) {
       this.innerHTML = "I am div number " + index;});
  }




Inbal Geffen
Change element content(2)

  callback functions
   ● each() gets a function that will work on any element in the array of divs
   ● The function gets the index of the element and it can use it

  function showDivNumber()
  {
       $("div").each(function (index) {
       this.innerHTML = "I am div number " + index;});
  }




Inbal Geffen
Change element attribute

   ● $("div").addClass("specialDiv");
  //add the class "specialDiv" to all the divs

   ● $("div").removeClass("specialDiv");
  //remove the class "specialDiv" from all the divs that has this class

   ● $("#div1").attr("id", "hello");
  //change the id of div with id="div1" to id="hello"




Inbal Geffen
Add elements

  <div id="div1">
       <p>I'm a paragraph</p>
  </div>

   ● $("<p style='color:red'>I am dynamic text</p>").appendTo("#div1");
  <div id="div1">
       <p>I'm a paragraph</p>
       <p style='color:red'>I am dynamic text</p>
  </div>

   ● $("<p style='color:red'>I am dynamic text</p>").prependTo("#div1");
  <div id="div1">
       <p style='color:red'>I am dynamic text</p>
       <p>I'm a paragraph</p>
  </div>

Inbal Geffen
Add elements(2)

  <div id="div1">
       <p>I'm a paragraph</p>
  </div>

   ● $("<p style='color:red'>I am dynamic text</p>").insertAfter("#div1");
  <div id="div1">
       <p>I'm a paragraph</p>
  </div>
  <p style='color:red'>I am dynamic text</p>




Inbal Geffen
Run the script

  We want to wait until all the page loads... and only then start our function

  <script type="text/javascript">
  $(document).ready(function () {
       $("<p style='color:red'>I am dynamic text</p>").appendTo("#div1");
  });
  </script>

  It also works using this shortcut:

  <script type="text/javascript">
  $(function () {
  $("<p style='color:red'>I am dynamic text</p>").appendTo("#div1");
  });
  </script>

Inbal Geffen
Events

  <input type="button" value="Test" />
  Bind(eventname, function to perform)        can be removed from the element using unbind()
  $("#btnTest").bind('click', function () {
  alert('btnTest was clicked');
  });

  Many events have shortcuts:
  $("#btnTest").click(function () {
  alert('btnTest was clicked');
  });

  Get info from the event
  $("#div1").mousemove(function(event) {
  var str = "(X = " + event.pageX + ", Y=" + event.pageY + ")";
  $("#div1").text(str);
  });
Inbal Geffen
Animations

  function divAnimate() {
  $("#div1").fadeOut();
  }

  <input type="button" value="Click Me" onclick="divAnimate();" />

  More animations
  ● $(“div1”).hide()
  ● $(“div1”).show()
  ● $(“div1”).toggle()
  ● $(“div1”).fadeOut()
  ● $(“div1”).fadeIn()
  ● $(“div1”).slideDown()
  ● $(“div1”).slideUp()


Inbal Geffen
Animations(2)

  Any css changes we want using the animate() method:

  $("#div1").animate({
  width: "100%",
  fontSize: "50px",
  }, 1500);

  Stop the animation:
  $("#div1").stop();



  jQuery supports multiple calls in a row
  $("#div1").fadeOut().slideDown().fadeOut().slideUp();



Inbal Geffen

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (18)

jQuery Presentation
jQuery PresentationjQuery Presentation
jQuery Presentation
 
Jquery
JqueryJquery
Jquery
 
J Query
J QueryJ Query
J Query
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
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
 
J Query Public
J Query PublicJ Query Public
J Query Public
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
jQuery in 15 minutes
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutes
 
Intro to jQuery
Intro to jQueryIntro to jQuery
Intro to jQuery
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
 
JQuery
JQueryJQuery
JQuery
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
jQuery, CSS3 and ColdFusion
jQuery, CSS3 and ColdFusionjQuery, CSS3 and ColdFusion
jQuery, CSS3 and ColdFusion
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
J query training
J query trainingJ query training
J query training
 
Jquery
JqueryJquery
Jquery
 
Add two numbers karan chanana
Add two numbers karan chananaAdd two numbers karan chanana
Add two numbers karan chanana
 

Andere mochten auch

Andere mochten auch (7)

Jquery2
Jquery2Jquery2
Jquery2
 
J queryui
J queryuiJ queryui
J queryui
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
 
Jquery mobile2
Jquery mobile2Jquery mobile2
Jquery mobile2
 
jQuery mobile UX
jQuery mobile UXjQuery mobile UX
jQuery mobile UX
 
Web Storage & Web Workers
Web Storage & Web WorkersWeb Storage & Web Workers
Web Storage & Web Workers
 

Ähnlich wie Learn jQuery in 40 Steps

Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQueryorestJump
 
Building iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" DominoBuilding iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" DominoRob Bontekoe
 
How to Mess Up Your Angular UI Components
How to Mess Up Your Angular UI ComponentsHow to Mess Up Your Angular UI Components
How to Mess Up Your Angular UI Componentscagataycivici
 
Rails Presentation - Technology Books, Tech Conferences
 Rails Presentation - Technology Books, Tech Conferences Rails Presentation - Technology Books, Tech Conferences
Rails Presentation - Technology Books, Tech Conferencestutorialsruby
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentationguestcf600a
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentationguestcf600a
 
JQuery-Tutorial" />
  JQuery-Tutorial" />  JQuery-Tutorial" />
JQuery-Tutorial" />tutorialsruby
 
HirshHorn theme: how I created it
HirshHorn theme: how I created itHirshHorn theme: how I created it
HirshHorn theme: how I created itPaul Bearne
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformanceJonas De Smet
 
The Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J QueryThe Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J QueryQConLondon2008
 
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 BasicsEPAM Systems
 
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Knowgirish82
 

Ähnlich wie Learn jQuery in 40 Steps (20)

Jquery 3
Jquery 3Jquery 3
Jquery 3
 
jQuery Presentasion
jQuery PresentasionjQuery Presentasion
jQuery Presentasion
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQuery
 
JQuery
JQueryJQuery
JQuery
 
Building iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" DominoBuilding iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" Domino
 
How to Mess Up Your Angular UI Components
How to Mess Up Your Angular UI ComponentsHow to Mess Up Your Angular UI Components
How to Mess Up Your Angular UI Components
 
Jquery introduction
Jquery introductionJquery introduction
Jquery introduction
 
Rails Presentation - Technology Books, Tech Conferences
 Rails Presentation - Technology Books, Tech Conferences Rails Presentation - Technology Books, Tech Conferences
Rails Presentation - Technology Books, Tech Conferences
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentation
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentation
 
JQuery-Tutorial" />
  JQuery-Tutorial" />  JQuery-Tutorial" />
JQuery-Tutorial" />
 
jQuery besic
jQuery besicjQuery besic
jQuery besic
 
HirshHorn theme: how I created it
HirshHorn theme: how I created itHirshHorn theme: how I created it
HirshHorn theme: how I created it
 
Jquery
JqueryJquery
Jquery
 
jQuery
jQueryjQuery
jQuery
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
The Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J QueryThe Dom Scripting Toolkit J Query
The Dom Scripting Toolkit J Query
 
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
 
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
 
J query1
J query1J query1
J query1
 

Learn jQuery in 40 Steps

  • 1. Practice jQuery Inbal Geffen
  • 2. Add jQuery to your project ● Download JQuery from the jQuery website and use this code : <script src=‘jquery.js’></script> OR ● Link to a CDN hosted jQuery and use a code like this : <script src=‘http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js’> </script> Inbal Geffen
  • 3. $("#div1") == jQuery("#div1") <script src="jquery.js" type="text/javascript"></script> <script type="text/javascript"> function hellojQuery() { $("#div1").html("Hello jQuery"); } </script> ● The $ sign tells us to choose an element from our code ● We can choose elements using all the known css selectors ● $("#div1") - will choose all the elements in the page with the id div1 ● .html - is like using innerHTML on all the selected elements Example Inbal Geffen
  • 4. Select Elements - CSS selectors ● $(“#div1”) - select all elements with "div1" id ● $(“div”) - select all div elements ● $(“.classic”) - select all elements with classic class ● $(“div.classic”) - select all div elements with classic class ● $(“div:last”) - select the last div ● $(“div:even”) - select all divs in even places and all the rest.. Inbal Geffen
  • 5. Select Elements - more selectors ● $(“:button”) - select all buttons ● $(“:contains(hello)”) - select all elements that contain the text "hello" ● $(“:hidden”) - select all hidden elements ● $(“:visible”) - select all visible elements ● $(“div:not(.classic)”) - select all elements that are not from classic class Inbal Geffen
  • 6. Select Elements - DOM tree <p id="p1"> This is a first link: <a href="page1.html">Page1</a><br /> This is a second link: <a href="page2.html">Page2</a><br /> This is a third link: <a href="page3.html">Page3</a><br /> </p> ● $(“p”).parent() - select the parent p ● $(“p”).children("a") - select all the a elements in the p ● $(“div”).siblings() - select the parent div ● $(“div”).next('div') - select the next div in the DOM ● $(“div”).prev('p') - select the previous p in the DOM Inbal Geffen
  • 7. What can we do with it? We used the $ to select elements from our page - what can we do with these elements? ● $(“div”)[0] - will return the first div element in the page ● $(“div”).size() - will return how many divs are in the page What else? ● Change the element attributes ● Change the element content ● Dynamically add elements to the page Inbal Geffen
  • 8. Change element content ● $(“div”).text("I am a div") - will change only the text in the div ● $(“div”).html(“I am a div”) - change the innerHTML of all the divs //This is instead of going in a loop over all the divs //We can iterate over each div using the each method function showDivNumber() { $("div").each(function (index) { this.innerHTML = "I am div number " + index;}); } Inbal Geffen
  • 9. Change element content(2) callback functions ● each() gets a function that will work on any element in the array of divs ● The function gets the index of the element and it can use it function showDivNumber() { $("div").each(function (index) { this.innerHTML = "I am div number " + index;}); } Inbal Geffen
  • 10. Change element attribute ● $("div").addClass("specialDiv"); //add the class "specialDiv" to all the divs ● $("div").removeClass("specialDiv"); //remove the class "specialDiv" from all the divs that has this class ● $("#div1").attr("id", "hello"); //change the id of div with id="div1" to id="hello" Inbal Geffen
  • 11. Add elements <div id="div1"> <p>I'm a paragraph</p> </div> ● $("<p style='color:red'>I am dynamic text</p>").appendTo("#div1"); <div id="div1"> <p>I'm a paragraph</p> <p style='color:red'>I am dynamic text</p> </div> ● $("<p style='color:red'>I am dynamic text</p>").prependTo("#div1"); <div id="div1"> <p style='color:red'>I am dynamic text</p> <p>I'm a paragraph</p> </div> Inbal Geffen
  • 12. Add elements(2) <div id="div1"> <p>I'm a paragraph</p> </div> ● $("<p style='color:red'>I am dynamic text</p>").insertAfter("#div1"); <div id="div1"> <p>I'm a paragraph</p> </div> <p style='color:red'>I am dynamic text</p> Inbal Geffen
  • 13. Run the script We want to wait until all the page loads... and only then start our function <script type="text/javascript"> $(document).ready(function () { $("<p style='color:red'>I am dynamic text</p>").appendTo("#div1"); }); </script> It also works using this shortcut: <script type="text/javascript"> $(function () { $("<p style='color:red'>I am dynamic text</p>").appendTo("#div1"); }); </script> Inbal Geffen
  • 14. Events <input type="button" value="Test" /> Bind(eventname, function to perform) can be removed from the element using unbind() $("#btnTest").bind('click', function () { alert('btnTest was clicked'); }); Many events have shortcuts: $("#btnTest").click(function () { alert('btnTest was clicked'); }); Get info from the event $("#div1").mousemove(function(event) { var str = "(X = " + event.pageX + ", Y=" + event.pageY + ")"; $("#div1").text(str); }); Inbal Geffen
  • 15. Animations function divAnimate() { $("#div1").fadeOut(); } <input type="button" value="Click Me" onclick="divAnimate();" /> More animations ● $(“div1”).hide() ● $(“div1”).show() ● $(“div1”).toggle() ● $(“div1”).fadeOut() ● $(“div1”).fadeIn() ● $(“div1”).slideDown() ● $(“div1”).slideUp() Inbal Geffen
  • 16. Animations(2) Any css changes we want using the animate() method: $("#div1").animate({ width: "100%", fontSize: "50px", }, 1500); Stop the animation: $("#div1").stop(); jQuery supports multiple calls in a row $("#div1").fadeOut().slideDown().fadeOut().slideUp(); Inbal Geffen