Home > Web Front-end > JS Tutorial > Detailed explanation of how to obtain pseudo-element (Pseudo-Element) attributes using JavaScript

Detailed explanation of how to obtain pseudo-element (Pseudo-Element) attributes using JavaScript

coldplay.xixi
Release: 2020-06-20 16:55:50
forward
3436 people have browsed it

Detailed explanation of how to obtain pseudo-element (Pseudo-Element) attributes using JavaScript

CSS pseudo-elements (pseudo-elements) are very useful - you can use it to create CSS triangles, use them on prompt boxes, and you can also use it to complete many simple tasks. Without the need for redundant HTML elements. Previously, CSS properties of pseudo-elements were not accessible with JavaScript, but now, there is a new JavaScript method to access them! Suppose your CSS code is like this:

.element:before {
	content: 'NEW';
	color: rgb(255, 0, 0);
}
Copy after login

In order to get the style attributes in .element:before, you can use the following JavaScript code:

var color = window.getComputedStyle(
	document.querySelector('.element'), ':before'
).getPropertyValue('color')
Copy after login

Replace the pseudo Element as the second parameter of the window.getComputedStyle method, you can get the attributes in the pseudo-element style! Put this tip in your knowledge base, and as browsers evolve, pseudo-elements will become more and more useful!

Recommended tutorial: "javascript basic tutorial"

The above is the detailed content of Detailed explanation of how to obtain pseudo-element (Pseudo-Element) attributes using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:webhek.com
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