If we need to remove the CSS from inline style rules, where should we put it? In a separate style sheet, of course! We can put the styles we want in a rule in our CSS that’s targeted to a given class, and use jQuery to add or remove that class from targeted elements in the HTML. Perhaps unsurprisingly, jQuery provides some handy methods for manipulating the class attributes of DOM elements. We’ll use the most common of these, addClass, to move our zebra stripe styles into the CSS file where they belong.
The addClass function accepts a string containing a class name as a parameter. You can also add multiple classes at the same time by separating the classnames with a space, just as you do when writing HTML:
$(‘div’).addClass(‘class_name’);
$(‘div’).addClass(‘class_name1 class_name2 class_name3′);
Adding and Removing Classes
Advertisement
This entry was posted in Uncategorized. Bookmark the permalink.