However, it is light in features when it comes to
many of the tasks required to handle desktop-like functionality in a web application. There??™s no
templating or the capability to work with data sets from within the library. If you need to add
some interactivity to your site, jQuery is a great solution.
Here??™s a quick example that helps demonstrate where jQuery really shines:
$("p.surprise").addClass("ohmy").show("slow");
You can probably see many similarities between this example and that for Prototype. First
and foremost, the important thing is how the dollar sign function accepts CSS selectors; with
Prototype you have to use the double dollar sign function. If you end up using each of these
libraries on different projects, this mistake might trip you up from time to time.
After the elements are retrieved, the example adds a class name of ohmy to each of the elements.
After that, the show() method animates the elements. In this case, the script will create
a slow slide out on each paragraph with a class name of surprise.
Event handling is similarly done with method chaining. In the following example, all
paragraph elements on the page are retrieved. Each one gets a click event attached to it. When
the event fires, it will retrieve the text from that element. The text() method is a method of
the jQuery object.
CHAPTER 4 n LIBRARIES 89
$("p").bind("click", function(){
alert( $(this).
Pages:
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152