
711
Working with JavaScript Frameworks
18
Once you’ve identified what’s to be affected, you provide an action or function. For example, if you
wanted to add a CSS class called
.highlight to all the <a> tags in your page, you would use code
like this:
$(“a”).addClass(“highlight”);
Note that the class name that is added does not include a leading period, so the attribute would be
written properly, that is,
class=”highlight” and not class=”.highlight”.
jQuery allows you to chain functions, one after the other. So, if you wanted to only add the
.high-
light
class to an <a> tag with a class of .current, you could write your code this way:
$(“a”).filter(“.current”).addClass(“highlight”);
Of course, jQuery can handle much more sophisticated effects. Let’s take a look at ...