HTML5 and CSS3, Second Edition

(singke) #1

APPENDIX 2


jQuery Primer


Writing clean and concise JavaScript that works well across all web browsers
is a difficult chore. Many libraries make it less painful, but jQuery is arguably
the most popular. It’s easy to use, has a wide array of existing plug-ins, and
is a good fit for easily creating fallback solutions and complex web applications.

This appendix introduces you to the parts of the jQuery library that we use
elsewhere in the book. It’s not meant to be a replacement for jQuery’s excellent
documentation,^1 nor is it an exhaustive list of the features and methods
available. It will, however, give you a good place to start.

jQuery makes Document Object Model (DOM) manipulation and event handling
a breeze. jQuery uses CSS selectors to locate elements, wraps those elements
in a special object, and then lets us alter the elements or add event listeners
to them. Everything jQuery does can be done with plain old JavaScript, but
jQuery’s advantage is that it handles cross-browser issues for us and makes
the syntax more uniform.

A2.1 Loading jQuery


We can grab the jQuery library from the jQuery website and link to the jQuery
script directly,^2 but we’ll load jQuery from Google’s servers, like this:

jqueryprimer/simple_selection.html
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

Browsers can make only a few connections to a server at a time. If we dis-
tribute our images and scripts to multiple servers, our users can download


  1. http://docs.jquery.com

  2. http://www.jquery.com


Download from Wow! eBook <www.wowebook.com> report erratum • discuss

Free download pdf