Top Banner
jQuery JavaScript Made Easy
24

jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

May 03, 2018

Download

Documents

vuongkhue
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

jQueryJavaScript Made Easy

Page 2: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

jQueryintheWild

• BankofAmerica

• Intuit

• Google

• NBC

• Slashdot

• Sourceforge

• WarnerBros.Records

• Newsweek

• Trac

• mozdev

• Drupal

• PEAR

• ZendFramework

• WordPress

• SPIP

• Symfony

Page 3: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

jQueryBenefits

• Frombeginnertoadvanced

• Atitscore,asimpleconcept

• Unobtrusivefromthegroundup

• Smallfilesize(s)

• EasyPluginAPI

• ExcellentdocumentaRon

• Thriving,Smart,HelpfulCommunity

Page 4: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

FindSomething,DoSomething

•$(‘tbodytr:nth‐child(even)’).addClass(‘alt’);

Page 5: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

Unobtrusive

jquery.js

custom.js

Behavior Content

index.html

Presentation

style.css

Page 6: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

DesignerlySyntax

• CSS

p{}

.menu{}

#content{}

• jQuery

$(‘p’)

$(‘.menu’)

$(‘#content’)

Page 7: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

DesignerlySyntax

• CSS

a[rel=nofollow]

#content>p{}

tr:nth‐child(even){}

• jQuery

$(‘a[rel=nofollow]’)

$(‘#content>p’)

$(‘tr:nth‐child(even)’)

Page 8: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

Example

•Gettheheightoftheviewport…

Page 9: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

var x,y;if (self.innerHeight) { // all except Explorer x = self.innerWidth; y = self.innerHeight;}else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode x = document.documentElement.clientWidth; y = document.documentElement.clientHeight;}else if (document.body) { // other Explorers x = document.body.clientWidth; y = document.body.clientHeight;}

DOMScripRng

Page 10: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

var x = $(window).width();var y = $(window).height();

jQuery

Page 11: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

Example

•Findablockquotewithaciteadribute<blockquotecite=“hdp://cnn.com”>…

•CreatealinktotheURLoftheciteadribute<ahref=“hdp://cnn.com”>source</a>

•Placethelinkinanewparagraphattheendoftheblockquote.

Page 12: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

DOMScripRngif (!document.getElementsByTagName) return false;var quotes = document.getElementsByTagName("blockquote");for (var i=0; i<quotes.length; i++) { var source = quotes[i].getAttribute("cite"); if (source) { var link = document.createElement("a"); link.setAttribute("href",source); var text = document.createTextNode("source"); link.appendChild(text); var para = document.createElement("p"); para.className = "attribution"; para.appendChild(link); quotes[i].appendChild(para); }}

Page 13: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

jQuery$('blockquote[cite]').each(function() { $('<p class="attribution"></p>') .append('<a href="' + $(this).attr('cite') + '">source</a>') .appendTo($(this));});

Page 14: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

GekngStarted

• ReferencingScriptsinHTML

• DocumentReady

Page 15: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

ReferencingScriptsinthe<head>

• Typicalsetup

• jQueryfilegoesfirst

<head><!-- stuff --><script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js" type="text/javascript"></script>

<script src="custom.js" type="text/javascript"></script><!-- more stuff --></head>

Page 16: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

DocumentReady

• BindsafuncRontobeexecutedassoonastheDOMhasbeensuccessfullyloaded...

• Beforeimagesload

• A.erstylesheetsload(fornow)

• Usuallybederthanwindow.onload

Page 17: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

$(document).ready(function() { // Stuff to do as soon as the DOM is ready;});

DocumentReady:Code

Page 18: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

DocumentReady:VarieRes

• $(document).ready(function() {/* */});

• $().ready(function() {/* */});

• $(function() {/* */});

• jQuery(function($) {/* /*});

Page 19: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

DocumentReady:

• MulRpledocumentreadyfuncRonsarefine.

• Scopemaybeanissue.

$(document).ready(function() { var mood = ‘happy’;});

$(document).ready(function() { try { alert(mood); // no alert } catch(err) { alert(err); // "ReferenceError: mood is not defined" }});

Page 20: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

DocumentReady:

• MulRpledocumentreadyfuncRonsarefine.

• Scopemaybeanissue.

$(document).ready(function() { var $.mood = ‘happy’;});

$(document).ready(function() { try { alert($.mood); // "happy" } catch(err) { alert(err); // no alert }});

Page 21: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

<body><!-- more stuff --><script src="jquery.js" type="text/javascript"></script><script type="text/javascript">

// no document ready here</script></body>

DocumentReady:

• When<script>isinside<body>(someRmes)

Page 22: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

$(window).bind('load', function() { // do something with images}

DocumentReadyWhenNottoUse

• Whenimagesneedtobeloadedfirst

Page 23: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

DocumentaRon

• OfficialDocumentaRon– API:docs.jquery.com

– FAQ

– Tutorials

• TutorialSites– learningjquery.com

– jqueryminute.com

– jqueryfordesigners.com

– manymore!

• Books– LearningjQuery

– jQueryinAcRon

–moreintheworks

Page 24: jQueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jQuery Benefits •From beginner to advanced •At its core, a simple concept •Unobtrusive from the ground up

TheCommunity

• jQuery“evangelism”• GoogleGroups

– jquery‐en– jquery‐dev– jquery‐ui

• planet.jquery.com• IRC

– freenode.net#jquery

• Twider–@jquery,@jquery‐ui