Home > Web Front-end > JS Tutorial > How jQuery modifies CSS pseudo-element attributes_jquery

How jQuery modifies CSS pseudo-element attributes_jquery

WBOY
Release: 2016-05-16 16:40:45
Original
1475 people have browsed it

CSS pseudo elements (pseudo elements) are not DOM elements, so you cannot select them directly.

Suppose there is the following HTML code:

<div class="techbrood" id="td_pseudo">techbrood introduction</div>
Copy after login

and CSS code:

.techbrood:before {
width: 0;
}
Copy after login

Now you want to dynamically set the width attribute of techbrood:before to 100% in the click event of an element,

There are two methods, one is to add a new style:

$('head').append("<style>.techbrood::before{ width:100% }</style>");
Copy after login

(Note that this method will affect all elements with class techbrood)

Another method is to add a new class to the element and set the attributes of the new class to achieve the effect of changing the attributes of the pseudo element:

.techbrood.change:before{
width: 100%;
}
Copy after login

jQuery code:

$('#td_pseudo').addClass("change");
Copy after login
Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template