+ All Categories
Home > Documents > J querypractice

J querypractice

Date post: 09-Jul-2015
Category:
Upload: inbal-geffen
View: 85 times
Download: 0 times
Share this document with a friend
16
Practice jQuery Inbal Geffen
Transcript
Page 1: J querypractice

Practice jQuery

Inbal Geffen

Page 2: J querypractice

Add jQuery to your project

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

OR

● Link to a CDN hosted jQueryand use a code like this :

<script src=‘http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js’></script>

Inbal Geffen

Page 3: J querypractice

<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

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

Inbal Geffen

Page 4: J querypractice

● $(“#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..

Select Elements - CSS selectors

Inbal Geffen

Page 5: J querypractice

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

Page 6: J querypractice

<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

Select Elements - DOM tree

Inbal Geffen

Page 7: J querypractice

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

Page 8: J querypractice

● $(“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 methodfunction showDivNumber(){

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

}

Change element content

Inbal Geffen

Page 9: J querypractice

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;});

}

Change element content(2)

Inbal Geffen

Page 10: J querypractice

● $("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"

Change element attribute

Inbal Geffen

Page 11: J querypractice

<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>

Add elements

Inbal Geffen

Page 12: J querypractice

<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>

Add elements(2)

Inbal Geffen

Page 13: J querypractice

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>

Run the script

Inbal Geffen

Page 14: J querypractice

<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);});

Events

Inbal Geffen

Page 15: J querypractice

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()

Animations

Inbal Geffen

Page 16: J querypractice

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();

Animations(2)

Inbal Geffen


Recommended