02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C H A P T E R 2<br />

■ ■ ■<br />

Common <strong>jQuery</strong> Actions <strong>and</strong><br />

Methods<br />

Now that you underst<strong>and</strong> how element selection works, you can start learning the basics of how <strong>jQuery</strong><br />

simplifies interaction with web pages. In this chapter, you’ll get your h<strong>and</strong>s dirty with the most common<br />

<strong>and</strong> useful aspects of <strong>jQuery</strong>.<br />

This chapter will read more like a reference <strong>and</strong> may be a bit dry at times, but it’s definitely in your<br />

best interest to work through the examples presented within. Having a basic underst<strong>and</strong>ing of how these<br />

methods work <strong>and</strong> what they do will prove invaluable as you start building the example project later on<br />

in this book.<br />

Underst<strong>and</strong>ing the Basic Behavior of <strong>jQuery</strong> Scripts<br />

One of the most convenient features of <strong>jQuery</strong> is the fact that nearly all its methods are chainable, which<br />

means methods can be executed one right after the other. This leads to clear, concise code that is easy to<br />

follow:<br />

$('p')<br />

.addClass('new-class')<br />

.text("I'm a paragraph!")<br />

.appendTo('body');<br />

Chainable methods are possible because each method returns the <strong>jQuery</strong> object itself after<br />

modification. At first, this concept may seem difficult to underst<strong>and</strong>, but as you work through the<br />

examples in this chapter, it should become clearer.<br />

Underst<strong>and</strong>ing <strong>jQuery</strong> Methods<br />

<strong>jQuery</strong> attempts to make several common programming tasks easier. At a glance, it simplifies JavaScript<br />

development <strong>by</strong> providing the following powerful tools:<br />

• DOM element selection using CSS syntax (which you learned in Chapter 1)<br />

• Simple traversal <strong>and</strong> modification of the DOM<br />

• Easy syntax for h<strong>and</strong>ling browser events (such as clicks <strong>and</strong> mouse-overs)<br />

25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!