Home >Web Front-end >CSS Tutorial >How Can I Change a TD Tag's Class with jQuery?
Change Class Name with jQuery
When dealing with dynamic web pages, you may need to alter specific elements by modifying their classes. This article explains how to change the class of a td tag based on its ID using jQuery.
To access the td element with a given ID, use the $('#td_id') selector. Once you have the element reference, you can use jQuery methods to modify its class.
Setting the Class:
To set the class regardless of its previous value, use .attr():
$('#td_id').attr('class', 'newClass');
Adding a Class:
To add a new class without affecting existing ones, use .addClass():
$('#td_id').addClass('newClass');
Toggling Classes:
To toggle between two classes, use .toggleClass():
$('#td_id').toggleClass('change_me newClass');
This will add newClass if it's currently not present and vice versa. Here's a reference to [jQuery's class attribute methods](https://api.jquery.com/category/attributes/class-attribute/) for further exploration.
The above is the detailed content of How Can I Change a TD Tag's Class with jQuery?. For more information, please follow other related articles on the PHP Chinese website!