Upload
marc-grabanski
View
274.785
Download
2
Embed Size (px)
Citation preview
divs = document.getElementByTagName(‘div’);
for (i = 0; i < divs.length; i++) {divs[i].style.display = ‘none’;
}
Hide divs with pure JavaScript
divs = document.getElementByTagName(‘div’);
for (i = 0; i < divs.length; i++) {divs[i].style.display = ‘none’;
}
Hide divs with pure JavaScript
$(“div”).hide();
Hide divs with jQuery
Selector Examples
$(“#content”) get element with id content
$(“li:first”) get first list item
$(“tr:odd”) get odd numbered table rows
Selector Examples
$(“#content”) get element with id content
$(“li:first”) get first list item
$(“tr:odd”) get odd numbered table rows
$(“a[target=_blank]”)get all links who’s target is “_blank”
Selector Examples
$(“#content”) get element with id content
$(“li:first”) get first list item
$(“tr:odd”) get odd numbered table rows
$(“form[id^=step]”)get all forms who’s id starts with “step”
$(“a[target=_blank]”)get all links who’s target is “_blank”
$(...).html();
One Method, Many Uses
$(...).html(“<p>hello</p>”);
$(...).html(function(i){
return “<p>hello “ + i + “</p>”;
});
jQuery Methods
•Moving Elements: append(), appendTo(), before(), after(),
•Attributescss(), attr(), html(), val(), addClass()
jQuery Methods
•Moving Elements: append(), appendTo(), before(), after(),
•Attributescss(), attr(), html(), val(), addClass()
•Eventsbind(), trigger(), unbind(), live(), click()
jQuery Methods
•Moving Elements: append(), appendTo(), before(), after(),
•Attributescss(), attr(), html(), val(), addClass()
•Eventsbind(), trigger(), unbind(), live(), click()
•Effectsshow(), fadeOut(), toggle(), animate()
jQuery Methods
•Moving Elements: append(), appendTo(), before(), after(),
•Attributescss(), attr(), html(), val(), addClass()
•Traversingfind(), is(), prevAll(), next(), hasClass()
•Eventsbind(), trigger(), unbind(), live(), click()
•Effectsshow(), fadeOut(), toggle(), animate()
jQuery Methods
•Moving Elements: append(), appendTo(), before(), after(),
•Attributescss(), attr(), html(), val(), addClass()
•Traversingfind(), is(), prevAll(), next(), hasClass()
•Eventsbind(), trigger(), unbind(), live(), click()
•Ajaxget(), getJSON(), post(), ajax(), load()
•Effectsshow(), fadeOut(), toggle(), animate()
jQuery Factory Method $()
You can also pass $() a functionto run the function after the page loads.
$(function(){
});
jQuery Factory Method $()
You can also pass $() a functionto run the function after the page loads.
$(function(){
});code here will execute after DOM is ready
jQuery Factory Method $()
You can also pass $() a functionto run the function after the page loads.
$(function(){
});
Note: This is essentially the same as..$(document).ready(function(){ });
code here will execute after DOM is ready
jQuery Factory Method $()
You can also pass $() a functionto run the function after the page loads.
$(function(){
});
Note: This is essentially the same as..$(document).ready(function(){ });
code here will execute after DOM is ready
..you will see this in tutorials around the net
$(“#foo”)
Get element with ID foo and add some HTML.
<html><body>
<div>jQuery</div><div id=”foo”>example</div>
</body></html>
Moving Elements Examples
$(“#foo”)
Get element with ID foo and add some HTML.
<html><body>
<div>jQuery</div><div id=”foo”>example</div>
</body></html>
Moving Elements Examples
.append(“<p>test</p>”);
$(“#foo”)
Get element with ID foo and add some HTML.
<html><body>
<div>jQuery</div><div id=”foo”>example<p>test</p></div>
</body></html>
Moving Elements Examples
.append(“<p>test</p>”);
<html><body>
<div>jQuery<p>moving</p> <p>paragraphs</p>
</div><div id=”foo”>example</div>
</body></html>
$(“p”)
Move paragraphs to element with id “foo”
Moving Elements Examples
<html><body>
<div>jQuery<p>moving</p> <p>paragraphs</p>
</div><div id=”foo”>example</div>
</body></html>
$(“p”)
Move paragraphs to element with id “foo”
Moving Elements Examples
.appendTo(“#foo”);
$(“p”)
Move paragraphs to element with id “foo”
<html><body>
<div>jQuery</div><div id=”foo”>example
<p>moving</p> <p>paragraphs</p>
</div></body>
</html>
Moving Elements Examples
.appendTo(“#foo”);
Attributes
.attr(‘id’)
Get
.html()
.val()
.html(“<p>hi</p>”)
Set
.attr(‘id’, ‘foo’)
.width()
.css(“top”)
Attributes
.attr(‘id’)
Get
.html()
.val()
.html(“<p>hi</p>”)
Set
.attr(‘id’, ‘foo’)
.val(“new val”)
.width()
.css(“top”)
Attributes
.attr(‘id’)
Get
.html()
.val()
.html(“<p>hi</p>”)
Set
.attr(‘id’, ‘foo’)
.val(“new val”)
.width()
.css(“top”) .css(“top”, “80px”)
Attributes
.attr(‘id’)
Get
.html()
.val()
.html(“<p>hi</p>”)
Set
.attr(‘id’, ‘foo’)
.val(“new val”)
.width() .width(60)
.css(“top”) .css(“top”, “80px”)
$(...).css(“border”, “1px solid black”); Set border to 1px black
Set various css properties.$(...).css({
“background”: “yellow”, “height”: “400px”
});
Attributes
$(...).css(“border”, “1px solid black”); Set border to 1px black
Set various css properties.$(...).css({
“background”: “yellow”, “height”: “400px”
});
$(“a”).attr(“href”, “http://google.com”);Set all link’s href attribute to google.com
Attributes
$(...).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>
$(...).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>
$(...).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>
$(...).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.
$(...).val();Get input value.
Attributes
<div>whatever</div> turns into<div><p>I’m new</p></div>
$(“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”).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.
$(“button”).live(‘click’, function(){something();
});
Event Delegation
Attach events to document
Attach event delegation to elements
$(“form“).delegate(“button”, ”click”, function(){something();
});
$(...).click(function(){$(“div:first”).slideToggle();
});
With each click, slide up / slide down a div.
Animation / Effects
$(...).click(function(){$(“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
$(...).click(function(){$(“div:first”).slideToggle();
});
With each click, slide up / slide down a div.
Animate elements to 300px wide in .5 seconds.$(...).animate({ “width”: “300px” }, 500);
$(...).fadeTo(500, 0.3);
Take focus off elements by fading them to 30% opacity in .5 seconds
Animation / Effects
$(“#myCell”)
Get previous table cells to #myCell.
<html><body>
<table><tr><td></td><td></td><td id=”myCell”></td><td></td>
</tr></table></body>
</html>
Traversing Examples
<html><body>
<table><tr><td></td><td></td><td id=”myCell”></td><td></td>
</tr></table></body>
</html>
$(“#myCell”)
Get previous table cells to #myCell.
Traversing Examples
.prevAll()
$(“#myCell”)
Get previous table cells to #myCell.
Traversing Examples
.prevAll()
<html><body>
<table><tr><td></td><td></td><td id=”myCell”></td><td></td>
</tr></table></body>
</html>
.andSelf();
<html><body>
<table></table><div>
<p>foo</p><span>bar</span>
</div></body>
</html>
$(“table”)
Move paragraphs to element with id “foo”
Traversing Examples
<html><body>
<table></table><div>
<p>foo</p><span>bar</span>
</div></body>
</html>
$(“table”)
Move paragraphs to element with id “foo”
Traversing Examples
.next()
$(“table”)
Move paragraphs to element with id “foo”
<html><body>
<table></table><div>
<p>foo</p><span>bar</span>
</div></body>
</html>
Traversing Examples
.next()
$(“table”)
Move paragraphs to element with id “foo”
Traversing Examples
.next().find(“p”);
<html><body>
<table></table><div>
<p>foo</p><span>bar</span>
</div></body>
</html>
$(...).get(“tag.php”, { “bar”: “baz” });
Post data, “bar” equals “baz” to tag.php using get.
Ajax Examples
$(...).get(“tag.php”, { “bar”: “baz” });
Post data, “bar” equals “baz” to tag.php using get.
Post data, “foo” equals “bar” to send.php, thenalert the response.
$.post(“send.php”, { foo: ”bar” }, function(response){
alert(response);});
Ajax Examples
$.fn.myPlugin = function(){return this.each(function(){
$(this).html(“you used myPlugin!”);});
});
<html><body>
<div></div><div></div>
</body></html>
Plugin Example
$.fn.myPlugin = function(){return this.each(function(){
$(this).html(“you used myPlugin!”);});
});
$(“div”).myPlugin();<html>
<body><div></div><div></div>
</body></html>
Plugin Example
$.fn.myPlugin = function(){return this.each(function(){
$(this).html(“you used myPlugin!”);});
});
<html><body>
<div>you used myPlugin!</div><div>you used myPlugin!</div>
</body></html>
$(“div”).myPlugin();
Plugin Example
jQuery isn’t only about simpler codeand being more productive
It is also about..great community
support tutorials
plugins
open (free) license
test coverage books
speed
light weight code
http://google.com/trends?q=dojo+javascript,+jquery+javascript,+yui+javascript,+prototype+javascript,+mootools+javascript&ctab=0&geo=all&date=all&sort=1
Led to World Domination
jQuery
Usage Across Top 10,000 Sites
http://trends.builtwith.com/javascript
Plugins
jQuery has hundreds of plugins athttp://plugins.jquery.com/
jQuery UI
Set of official user interface components at:http://jqueryui.com
http://forum.jquery.com
Support
http://docs.jquery.com/Discussion
jQuery general discussion mailing list
jQuery discussion docs page
jQuery IRC room#jquery on FreeNode.net
Books
http://www.amazon.com/gp/product/1847196705?ie=UTF8&tag=jacofalltrawe-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1847196705
http://www.amazon.com/gp/product/1933988355?ie=UTF8&tag=jacofalltrawe-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1933988355
Learning jQuery 1.3by Karl Swedberg
jQuery in ActionYahuda Katz
http://marcgrabanski.com/article/the-jquery-course-prerelease
Video Training
Thank you!
Marc Grabanski: http://marcgrabanski.com
Twitter: @1Marc