Modifying and Removing CSS Class Definitions Dynamically
Adding CSS classes at runtime is a known JavaScript manipulation, but extending its functionality to modifying or removing existing class definitions may seem daunting. This article addresses this challenge by exploring ways to achieve these operations programmatically.
Changing CSS Class Definitions
To edit a CSS class definition, follow these steps:
For example, to modify the font size of the .menu class:
let styleSheet = document.styleSheets[0]; let rule = styleSheet.cssRules[1]; // Assuming ".menu" is the second rule rule.style.setProperty('font-size', '15px');
Removing CSS Class Definitions
To remove a CSS class definition, follow these steps:
For example, to remove the .menu class definition:
let styleSheet = document.styleSheets[0]; let rule = styleSheet.cssRules[1]; // Assuming ".menu" is the second rule stylesheet.deleteRule(1);
The above is the detailed content of How Can I Dynamically Modify and Remove CSS Class Definitions with JavaScript?. For more information, please follow other related articles on the PHP Chinese website!