jqueryexamples.learningjquery.com/presentations/bcgr3-jquery.pdf · jquery benefits •from...

24
jQuery JavaScript Made Easy

Upload: vuongkhue

Post on 03-May-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

jQueryJavaScript Made Easy

jQueryintheWild

• BankofAmerica

• Intuit

• Google

• NBC

• Slashdot

• Sourceforge

• WarnerBros.Records

• Newsweek

• Trac

• mozdev

• Drupal

• PEAR

• ZendFramework

• WordPress

• SPIP

• Symfony

jQueryBenefits

• Frombeginnertoadvanced

• Atitscore,asimpleconcept

• Unobtrusivefromthegroundup

• Smallfilesize(s)

• EasyPluginAPI

• ExcellentdocumentaRon

• Thriving,Smart,HelpfulCommunity

FindSomething,DoSomething

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

Unobtrusive

jquery.js

custom.js

Behavior Content

index.html

Presentation

style.css

DesignerlySyntax

• CSS

p{}

.menu{}

#content{}

• jQuery

$(‘p’)

$(‘.menu’)

$(‘#content’)

DesignerlySyntax

• CSS

a[rel=nofollow]

#content>p{}

tr:nth‐child(even){}

• jQuery

$(‘a[rel=nofollow]’)

$(‘#content>p’)

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

Example

•Gettheheightoftheviewport…

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

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

jQuery

Example

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

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

•Placethelinkinanewparagraphattheendoftheblockquote.

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

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

GekngStarted

• ReferencingScriptsinHTML

• DocumentReady

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>

DocumentReady

• BindsafuncRontobeexecutedassoonastheDOMhasbeensuccessfullyloaded...

• Beforeimagesload

• A.erstylesheetsload(fornow)

• Usuallybederthanwindow.onload

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

DocumentReady:Code

DocumentReady:VarieRes

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

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

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

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

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

DocumentReady:

• MulRpledocumentreadyfuncRonsarefine.

• Scopemaybeanissue.

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

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

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

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

DocumentReadyWhenNottoUse

• Whenimagesneedtobeloadedfirst

DocumentaRon

• OfficialDocumentaRon– API:docs.jquery.com

– FAQ

– Tutorials

• TutorialSites– learningjquery.com

– jqueryminute.com

– jqueryfordesigners.com

– manymore!

• Books– LearningjQuery

– jQueryinAcRon

–moreintheworks

TheCommunity

• jQuery“evangelism”• GoogleGroups

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

• planet.jquery.com• IRC

– freenode.net#jquery

• Twider–@jquery,@jquery‐ui