首页 > 解决方案 > 如何通过 DOM 样式在 JavaScript 中调用 ::before

问题描述

我需要通过 DOM 样式更改 javaScript 中的伪元素 (::before) 样式。我怎样才能做到这一点?

像这样

document.getElementById("new-store-nav").style.color= "#000";

我已经通过调用 ID 改变了这种风格,但是如何在 javascript 中调用 ::before?

标签: javascripthtmljquerycss

解决方案


您需要使用对象getComputedStyle上的功能window

在你的情况下,它会像:

const color = window.getComputedStyle(
    document.getElementById("new-store-nav"), ':before'
).getPropertyValue('color')

参考大卫沃尔什的网站在这里


推荐阅读