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

How jQuery modifies CSS pseudo-element attributes_jquery

WBOY
WBOYOriginal
2016-05-16 16:40:451455browse

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>

and CSS code:

.techbrood:before {
width: 0;
}

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>");

(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%;
}

jQuery code:

$('#td_pseudo').addClass("change");
Statement:
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