简体   繁体   English

使用JS覆盖CSS类

[英]Override CSS class using JS

I am using Reveal JS. 我正在使用Reveal JS。 This is the CSS that I want to override: 这是我要覆盖的CSS:

.reveal section img {...

Using one of the methods below (or something better), how can I use JS to override that? 使用下面的一种方法(或更好的方法),如何使用JS覆盖它?

document.getElementById(image1).classList.add("red"); // I tried creating a CSS class called "red" but it doesn't overide the original
document.getElementById(image1).style.boxShadow = "red"; // this doesn't overide the original theme CSS

First, use chrome dev tools to make sure that the class is actually being added to the element. 首先,使用chrome dev工具确保该类实际上已添加到元素中。

If it is being added but the styles are not being applied, your problems may be stemming from css specificity rules . 如果添加了样式但未应用样式,则您的问题可能源于CSS特殊性规则

Instead of defining your class like this: 而不是像这样定义您的班级:

.red {
  box-shadow: 5px 2px 2px red;
}

Try this: 尝试这个:

.reveal section img.red {
  box-shadow: 5px 2px 2px red;
}

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

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