javascript - Javascript 如何在 :active 伪元素上使用 getComputedStyle?
问题描述
我曾尝试将 getComputedStyle 与其他伪元素一起使用,它们都有效。但是当我尝试获取 :active 的属性时,它总是读取默认值。
function myFunction() {
var elem = document.getElementById("test");
var theCSSprop = window.getComputedStyle(elem, ":active").getPropertyValue("font-size");
document.getElementById("demo").innerHTML = theCSSprop;
}
div {
cursor: pointer
}
div:active {
font-size: 50pt;
}
<div id="test" style="height: 50px;background-color: yellow;">Test Div</div>
<p><button onclick="myFunction()">Show :active font-size</button></p>
<p>The computed font size for div:active in the test div is: <span id="demo"></span></p>
有没有办法做到这一点?
解决方案
按照规范 getComputedStyle 只接受伪元素作为第二个参数。 https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
但是, :active 不是伪元素,而是伪类。
推荐阅读
- flutter - 如何制作一个像这样推动另一个屏幕的屏幕?(例如:不和谐的应用程序)
- python - 将大型 PostgreSQL 表读入 Pandas 数据框时出现问题
- rust - 一个以 trait 作为参数的 Rust 闭包
- android - 将我的颤振应用程序添加到 android 中的超省电模式
- c++ - 用 C++ 编译 ImGui
- docker - 为什么 Docker 守护进程无法访问 localhost 地址?
- machine-learning - 如何将 MultiOutputClassifier 应用于 Naive-Bayes 算法的数据集
- parallel-processing - 并行运行地图切片
- r - 如何根据R中第二个变量的排名将变量的排名分成小数,以打破平局
- mongodb - 如何高效地执行多个 MongoDB 查询,每次都删除搜索参数?