繁体   English   中英

使用Javascript应用CSS

[英]Apply CSS using Javascript

如何在JavaScript中应用类似于以下CSS的元素:

.watch-non-stage-mode .player-width {
    width : 640px !important;
}

我尝试这样做,但无法正常工作:

document.getElementByClassName('.watch-non-stage-mode .player-width').style.width = '640px !important';

尝试使用querySelector

document.querySelector('.watch-non-stage-mode .player-width').style.width = '640px'

!important是否必需:

document.querySelector('.watch-non-stage-mode .player-width').style.cssText = 'width: 640px !important;'

在您的情况下,您也可以使用getElementsByClassName ,但是您错误地获得了一个节点,必须是:

document.getElementsByClassName('watch-non-stage-mode player-width')[0]

必须将“ px”和“!important”单位分别添加到属性中。 做:

document.querySelector('.watch-non-stage-mode .player-width').style.width = 640 + 'px' + '!important';

另外,请按照说明使用querySelector通过实际的点类名选择元素。 您不能以显示方式使用“ .classname”(无论如何还是不正确的)。 元素是复数。 getElement ByClassName

暂无
暂无

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

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