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
coldplay.xixiforward
2020-06-20 16:55:503468browse

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

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')

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!

Statement:
This article is reproduced at:webhek.com. If there is any infringement, please contact admin@php.cn delete