Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

526 LESSON 18: Using jQuery


jQuery you want to use on your site the way you would any other script file. For
example, to link to the 2.1.4 version you would write: <script src="https://
code.jquery.com/jquery-2.1.4.min.js"></script>.
Q What about users who don’t have JavaScript enabled?
A It’s generally agreed that less than 3% of users have JavaScript disabled these days.
However, that is still a lot of people, so you’ll want to make sure that essential
functionality still works for users who don’t have JavaScript access. That’s one of
the big advantages of the unobtrusive JavaScript approach that jQuery reinforces.
The markup should work fine without the JavaScript, which enhances the experi-
ence but is not essential to making the pages work.

Quiz



  1. What is the purpose of jQuery’s document.ready() handler?

  2. How do I select an item with the ID navigationLink in jQuery?

  3. What does the variable this refer to in an event-handling function?


Quiz Answers



  1. The document.ready() event, provided by jQuery, fires when the HTML docu-
    ment is fully constructed and before resources like images are loaded. It’s generally
    a safe point at which you can begin initializing JavaScript for a page.

  2. jQuery uses CSS-style selectors. So to select an item with the ID navigationLink,
    you’d use $("#navigationLink").

  3. In an event handler, this refers to the object that the event was fired from. So if
    it’s a click event on a link, this refers to the link that was clicked.


Exercises



  1. Download jQuery and use the