添加带有样式组件的活动类不起作用

Jah*_*ony 2 javascript css reactjs gatsby styled-components

所以我的功能是:

function onClickChange(props) {
  let MyDiv = document.getElementById('myDIV')
  let InterfaceDesign = document.getElementById('interfaceDesign')
  if (MyDiv.style.display === 'none') {
    MyDiv.style.display = ''
    InterfaceDesign.classList.add('active')
  } else {
    MyDiv.style.display = 'none'
    InterfaceDesign.classList.remove('active')
  }
}
Run Code Online (Sandbox Code Playgroud)

从 DOM 中的这个函数我可以看到它正在获取活动类。但是从我的样式组件:

const FirstDivElement = styled.div`
  position: relative;
  overflow: hidden;
  width: 100%;
  background-color: #000;
  &:hover {
    background-color: #e6007e;
    transform: scale(1.05);
  }
  &:active{
    background-color: #e6007e;
  }
`
Run Code Online (Sandbox Code Playgroud)

它没有获得 &:active 的风格

我的 div 元素是:

<div id="interfaceDesign">
            <div onClick={onClickChange}>
              <ListItems
                headingText="Interface Design"
                backgroundImage={props.secondBackgroundImage}
              >
                <Img
                  style={{
                    height: '50px',
                    width: '50px',
                  }}
                  sizes={props.interfacedesign}
                />
              </ListItems>
            </div>
          </div>
          <div id="myDIV" style={{ display: 'none' }}>
            <InterfaceDesign
              secondBackgroundImage={props.secondBackgroundImage}
              interfacedesignMagenta={props.interfacedesignMagenta}
            />
          </div>
          </div>
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?提前谢谢你:D

cor*_*ard 5

您正在为:active 伪类提供样式,它仅在元素被激活时使用(例如在鼠标单击期间)。您可能希望将样式化组件 CSS 中的from 更改&:active&.active

此外,您应该注意,您通常会根据带有样式化组件的道具更改样式。你可以做你正在做的事情,但不太常见。