简体   繁体   English

Javascript 如何在 an:active 伪元素上使用 getComputedStyle?

[英]Javascript how to use getComputedStyle on an :active pseudo element?

I have tried to use getComputedStyle with other pseudo elements and they all worked.我曾尝试将 getComputedStyle 与其他伪元素一起使用,它们都有效。 But when I try to get the property of:active it always reads the default value.但是当我尝试获取属性时:活动它总是读取默认值。

 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>

Is there a way to do this?有没有办法做到这一点?

By specification getComputedStyle only accepts pseudo-elements as the second parameter.按照规范 getComputedStyle 只接受伪元素作为第二个参数。 https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

However, :active is not a pseudo-element but a pseudo-class.但是, :active 不是伪元素,而是伪类。

https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Pseudo-classes_and_pseudo-elements https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Pseudo-classes_and_pseudo-elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM