Home Web Front-end JS Tutorial JavaScript implements modifying pseudo-class styles

JavaScript implements modifying pseudo-class styles

Dec 07, 2017 pm 03:58 PM
javascript js style

In projects, we often need to use JavaScript to dynamically control the style of elements (:before,:after), but we all know that JavaScript or jQuery does not have pseudo-class selectors. In this article, we mainly introduce the method of modifying pseudo-class styles in JavaScript and the code implementation process.

HTML

Hi, this is a plain-old, sad-looking paragraph tag.

CSS


.red::before {
content: 'red';
color: red;
}
Copy after login


Method 1

Use JavaScript or jQuery to switch

The class name of the element, modify the style.


.green::before {
content: 'green';
color: green;
}
$('p').removeClass('red').addClass('green');
Copy after login


Method 2

Dynamically insert new styles into existing