javascript - 如何通过 DOM 样式在 JavaScript 中调用 ::before
问题描述
我需要通过 DOM 样式更改 javaScript 中的伪元素 (::before) 样式。我怎样才能做到这一点?
像这样
document.getElementById("new-store-nav").style.color= "#000";
我已经通过调用 ID 改变了这种风格,但是如何在 javascript 中调用 ::before?
解决方案
您需要使用对象getComputedStyle
上的功能window
在你的情况下,它会像:
const color = window.getComputedStyle(
document.getElementById("new-store-nav"), ':before'
).getPropertyValue('color')
参考大卫沃尔什的网站在这里
推荐阅读
- c++ - 使用自定义 cpp 源文件而不将其作为命令行参数提供给 g++,例如 iostream
- google-apps-script - Mandrill API 到 Google 表格脚本
- splunk - 您能否组合多值字段以形成整合的 Splunk 警报?
- javascript - Heroku - 部署时显示空白屏幕的 Express 和 React 应用程序
- python - 试图在长字符串中查找某些字符的组合
- c# - 如何将 json 对象作为 C# 中脚本的参数传递给 PowerShell 实例?
- sql - 如何在它们不靠近时合并多个选择语句?
- r - 高效绘制层次集群的一部分
- javascript - 如何合并两个 JSON,其中一个 JSON 将具有实际响应,另一个将提供配置
- java - 如何使用 aws java SDK 调用使用 Cognito 隐式授权授权的 api 端点