Selector Examples$“li:first” get first list item... Selector Examples$“li:first” get first list item... jQuery Factory Method $You can also pass $ a functionto run the function after the
Trang 1jQuery Essentials
by Marc Grabanski
v2
Trang 2We needed a hero to get these guys in line
Trang 3jQuery rescues us by working the same in all browsers!
Trang 4Easier to write jQuery than pure JavaScript
Trang 7HTML is tied to JavaScript
Trang 8jQuery Philosophy
Trang 9#1 Find some HTML jQuery Philosophy
Trang 10#1 Find some HTML
#2 Do something to it jQuery Philosophy
Trang 11Find
Trang 12elements
Trang 13Give $() a selector
Trang 14Give $() a selector
$(“ #myId ”)
Trang 15Give $() a selector
$(“ #myId ”) $(“ myClass ”)
Trang 16Give $() a selector
$(“ #myId ”) $(“ myClass ”) $(“ table ”)
Trang 17Selector Examples
Trang 18Selector Examples
$(“li:first”) get first list item
Trang 19Selector Examples
$(“li:first”) get first list item
Trang 20Selector Examples
$(“li:first”) get first list item
get all links who’s target is “_blank”
Trang 21Selector Examples
$(“li:first”) get first list item
Trang 22You can also string selectors together
Trang 23You can also string selectors together
$(“ #myId, myClass, table ”)
Trang 24Find
Trang 25Do addClass(“ redbox ”);
$(“div”)
Find
Trang 26jQuery API Spice
Two things that make the API HOT
Trang 27Chain Methods
Trang 28Chain Methods
.fadeOut();
Trang 29$( ) html();
One Method, Many Uses
Trang 30$( ) html();
One Method, Many Uses
Trang 32jQuery Methods
Trang 33• Moving Elements:
append (), appendTo (), before (), after (),
Trang 39jQuery Factory Method $()You can also pass $() a function
to run the function after the page loads
Trang 40jQuery Factory Method $()
You can also pass $() a function
to run the function after the page loads
});
Trang 41jQuery Factory Method $()
You can also pass $() a function
to run the function after the page loads
});
code here will execute after DOM is ready
Trang 42jQuery Factory Method $()
You can also pass $() a function
to run the function after the page loads
});
Note: This is essentially the same as
code here will execute after DOM is ready
Trang 43jQuery Factory Method $()
You can also pass $() a function
to run the function after the page loads
});
Note: This is essentially the same as
code here will execute after DOM is ready
you will see this in tutorials around the net
Trang 47Move paragraphs to element with id “foo”
Moving Elements Examples
Trang 48Move paragraphs to element with id “foo”
Moving Elements Examples
.appendTo(“#foo”);
Trang 50Attributes
Trang 51Get
Trang 52.attr(‘id’)
Get
.html()
Trang 58.attr(‘id’, ‘foo’)
.val(“new val”)
.width()
.css(“top”)
Trang 59.attr(‘id’, ‘foo’)
.val(“new val”)
.width()
.css(“top”) .css(“top”, “80px”)
Trang 60.attr(‘id’, ‘foo’)
.val(“new val”)
.width() .width(60)
.css(“top”) .css(“top”, “80px”)
Trang 61Attributes
Trang 62$( ) css (“ border ”, “ 1px solid black ”); Set border to 1px black
Attributes
Trang 63$( ) css (“ border ”, “ 1px solid black ”); Set border to 1px black
Set various css properties.
Trang 64$( ) css (“ border ”, “ 1px solid black ”);
Set border to 1px black
Set various css properties.
Trang 66$( ) html (“ <p>I’m new</p> ”);
Replace HTML with a new paragraph.
Attributes
Trang 67$( ) html (“ <p>I’m new</p> ”);
Replace HTML with a new paragraph.
Attributes
<div>whatever</div> turns into
<div> <p>I’m new</p> </div>
Trang 68$( ) html (“ <p>I’m new</p> ”);
Replace HTML with a new paragraph.
Set checkboxes attribute “checked” to checked.
$(“ :checkbox ”) attr (“ checked ”,” checked ”);
Attributes
<div>whatever</div> turns into
<div> <p>I’m new</p> </div>
Trang 69$( ) html (“ <p>I’m new</p> ”);
Replace HTML with a new paragraph.
Set checkboxes attribute “checked” to checked.
$(“ :checkbox ”) attr (“ checked ”,” checked ”);
$( ) val (“ 3 ”);
Set input value to 3.
Attributes
<div>whatever</div> turns into
<div> <p>I’m new</p> </div>
Trang 70$( ) html (“ <p>I’m new</p> ”);
Replace HTML with a new paragraph.
Set checkboxes attribute “checked” to checked.
$(“ :checkbox ”) attr (“ checked ”,” checked ”);
<div>whatever</div> turns into
<div> <p>I’m new</p> </div>
Trang 71Events Examples
Trang 72Events
Trang 73$(“ button ”) click ( function(){
something();
} );
When a button is clicked, do something
Events
Trang 74$(“ button ”) click ( function(){
something();
} );
When a button is clicked, do something
Setup a custom event and trigger it.
$(“ button “) bind (“ expand ”, function(){ something();
});
$(“ button:first “) trigger (“ expand ”);
Events
Trang 75$(“ button ”) click ( function(){
something();
} );
When a button is clicked, do something
Setup a custom event and trigger it.
$(“ button “) bind (“ expand ”, function(){ something();
});
$(“ button:first “) trigger (“ expand ”);
Events
$(“ button “) unbind (“ expand ”);
Unbind custom event.
Trang 76Event Delegation
Trang 77$(“ button ”) live (‘ click ’, function(){
Trang 78$(“ button ”) live (‘ click ’, function(){
something();
} );
Event Delegation
Attach events to document
Attach event delegation to elements
$(“ form “) delegate (“ button ”, ” click ”, function(){ something();
});
Trang 79Effects / Animation
Examples
Trang 80Animation / EffectsTypes of Effects
Trang 81Animation / Effects
#1 Hide and ShowTypes of Effects
Trang 82Animation / Effects
#2 Fade In and Out
#1 Hide and ShowTypes of Effects
Trang 83Animation / Effects
#2 Fade In and Out
#1 Hide and Show
#3 Slide Up and Down Types of Effects
Trang 84Animation / Effects
Trang 86$(“ div:first ”) slideToggle ();
});
With each click, slide up / slide down a div.
Animate elements to 300px wide in 5 seconds.
$( ) animate ({ “ width ”: “ 300px ” }, 500 );
Animation / Effects
Trang 87$(“ div:first ”) slideToggle ();
});
With each click, slide up / slide down a div.
Animate elements to 300px wide in 5 seconds.
Trang 88Traversing Examples
Trang 96Ajax Examples
Trang 97Ajax Examples
Trang 98$( ) get (“tag.php”, { “ bar ”: “ baz ” });
Post data, “bar” equals “baz” to tag.php using get.
Ajax Examples
Trang 99$( ) get (“tag.php”, { “ bar ”: “ baz ” });
Post data, “bar” equals “baz” to tag.php using get.
Post data, “foo” equals “bar” to send.php, then
alert the response
$ post (“ send.php ”, { foo : ” bar ” },
function(response){
alert(response);
} );
Ajax Examples
Trang 100Extending jQuery
Trang 103<div> you used myPlugin! </div>
<div> you used myPlugin! </div>
</body>
</html>
$(“div”).myPlugin();
Plugin Example
Trang 104Wait, There’s More!
Trang 105jQuery isn’t only about simpler code
Trang 106jQuery isn’t only about simpler code
Trang 107jQuery isn’t only about simpler code
It is also about
Trang 108jQuery isn’t only about simpler code
It is also about
great community
pluginsopen (free) license
speed
light weight code
Trang 109+javascript,+mootools+javascript&ctab=0&geo=all&date=all&sort=1
Led to World Domination
jQuery
Trang 110Usage Across Top 10,000 Sites
http://trends.builtwith.com/javascript
Trang 114Video Training
Trang 115Thank you!
Marc Grabanski:
http://marcgrabanski.com