服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - js 动态修改css文件的方法

js 动态修改css文件的方法

2021-02-04 16:32whsnow JavaScript

css文件可以用js 动态修改,在做一些交互动作时,非常实用,下面是具体示例,大家可以看看

?
1
2
3
4
5
6
7
8
9
10
11
12
_.find(document.styleSheets[4].cssRules,function(cssRule){
if(cssRule.selectorText && cssRule.selectorText.indexOf(".navbar-fixed-top2")>-1){
cssRule.style.position="";
cssRule.style.top = "0px";
}
if(cssRule.selectorText && cssRule.selectorText.indexOf("#pageIndi_content, #page1_l1_content, #page_appList") >-1){
cssRule.style.padding = "0px";
}
if(cssRule.selectorText && cssRule.selectorText.indexOf("#page1_index_content") >-1){
cssRule.style.padding = "0px";
}
});

循环用的underscore,在使用cssRule不能直接使用下面的代码,否则不会生效

?
1
cssRule.style=" left: 0;position:'';right: 0; top: 0px;z-index: 1031;";

只能使用

?
1
cssRule.style.padding = "0px";

延伸 · 阅读

精彩推荐