简体   繁体   English

如何在JavaScript中的元素上取消设置:active状态?

[英]How do I unset the :active state on an element in JavaScript?

I have an element which, when you click on it, shows an inner element sliding up using CSS transitions. 我有一个元素,当您单击它时,它显示使用CSS过渡向上滑动的内部元素。 I do this by changing the inner element's height in CSS' :active on the parent node. 我通过在父节点上更改CSS' :active内部元素的高度来实现。 I cannot figure out how to unset this afterwards. 我无法弄清楚以后如何取消此设置。 None of the following work: 没有以下工作:

/* NONE OF THESE WORK */
//Attempt 1
document.activeElement = null;

//Attempt 2
document.activeElement.blur();

//Attempt 3
document.body.focus();

//Attempt 4
document.body.click();

//Attempt 5
myElement.blur();

Is there a way to make it not active? 有没有办法使它不活跃?

You can remove CSS :active pseudoclass using element.classList.remove("hasactive"); 您可以使用element.classList.remove("hasactive");删除CSS :active伪类element.classList.remove("hasactive");

 var inner = document.getElementById("inner"); inner.addEventListener("transitionend", function(evt){ this.parentNode.classList.remove("hasactive"); }) 
 #outer:active #inner { height: 200px; } #inner { background: red; height: 0; transition: height 1s linear; -webkit-transition: height 1s linear; } #outer { height: 30px; background:blue; } 
 <div id="outer"> <div id="inner"> </div> </div> 

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

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