jQuery in Action

Changing element styling
59
space-delimited string. How disappointing, and how cumbersome! This means
that whenever we want to add class names to or remove class names from an
element that already has class names, we need to parse the string to determine
the individual names when reading it and be sure to restore it to valid space-
delimited format when writing it.
Although it's not a monumental task to write code to handle all that, it's always
a good idea to abstract such details behind an API that hides the mechanical
details of such operations. Luckily, jQuery has already done that for us.
Adding class names to all the elements of a matched set is an easy operation
with the following
addClass()
command:
Removing class names is as straightforward with the following
removeClass()
command:
Often, we may want to switch a set of styles back and forth, perhaps to indicate a
change between two states or for any other reasons that make sense with our
interface. jQuery makes it easy with the
toggleClass()
command.
Command syntax: addClass
addClass(names)
Adds the specified class name or class names to all elements in the wrapped set
Parameters
names
(String) A string containing the class name to add or, if multiple class names are
to be added, a space-delimited string of class names
Returns
The wrapped set
Command syntax: removeClass
removeClass(names)
Removes the specified class name or class names from each element in the wrapped set
Parameters
names
(String) A string containing the class name to remove or, if multiple class names
are to be removed, a space-delimited string of class names
Returns
The wrapped set


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.