JS设置CSS样式的几种方式
用 JS 来动态设置 CSS 样式,常见的有以下几种
1. 直接设置 style 的属性 某些情况用这个设置 !important 值无效
如果属性有 '-' 号,就写成驼峰的形式(如 textAlign) 如果想保留 - 号,就中括号的形式 element.style['text-align'] = '100px';
element.style.height = '100px';
2. 直接设置属性(只能用于某些属性,相关样式会自动识别)
element.setAttribute('height', 100);
element.setAttribute('height', '100px');
3. 设置 style 的属性
element.setAttribute('style', 'height: 100px !important');
4. 使用 setProperty 如果要设置!important,推荐用这种方法设置第三个参数
element.style.setProperty('height', '300px', 'important');
5. 改变 class 比如 JQ 的更改 class 相关方法
因 JS 获取不到 css 的伪元素,所以可以通过改变伪元素父级的 class 来动态更改伪元素的样式
element.className = 'blue';
element.className += 'blue fb';
6. 设置 cssText
element.style.cssText = 'height: 100px !important';
element.style.cssText += 'height: 100px !important';
7. 创建引入新的 css 样式文件
function addNewStyle(newStyle) {
var styleElement = document.getElementById('styles_js');
<span style="color: rgba(0, 0, 255, 1)">if (!<span style="color: rgba(0, 0, 0, 1)">styleElement) {
styleElement = document.createElement('style'<span style="color: rgba(0, 0, 0, 1)">);
styleElement.type = 'text/css'<span style="color: rgba(0, 0, 0, 1)">;
styleElement.id = 'styles_js'<span style="color: rgba(0, 0, 0, 1)">;
document.getElementsByTagName('head')[0<span style="color: rgba(0, 0, 0, 1)">].appendChild(styleElement);
}
styleElement.appendChild(document.createTextNode(newStyle));
}
addNewStyle('.box {height: 100px !important;}');</span></span></span></span></span></span></span></span></span></span></pre>
8. 使用 addRule、insertRule
// 在原有样式操作
document.styleSheets[0].addRule('.box', 'height: 100px');
document.styleSheets[0].insertRule('.box {height: 100px}', 0);
<span style="color: rgba(0, 128, 0, 1)">//<span style="color: rgba(0, 128, 0, 1)"> 或者插入新样式时操作
<span style="color: rgba(0, 0, 255, 1)">var styleEl = document.createElement('style'<span style="color: rgba(0, 0, 0, 1)">),
styleSheet =<span style="color: rgba(0, 0, 0, 1)"> styleEl.sheet;
styleSheet.addRule('.box', 'height: 100px'<span style="color: rgba(0, 0, 0, 1)">);
styleSheet.insertRule('.box {height: 100px}', 0<span style="color: rgba(0, 0, 0, 1)">);
document.head.appendChild(styleEl); </span></span></span></span></span></span></span></span></span></span></span></pre>
用 JS 来动态设置 CSS 样式,常见的有以下几种
1. 直接设置 style 的属性 某些情况用这个设置 !important 值无效
如果属性有 '-' 号,就写成驼峰的形式(如 textAlign) 如果想保留 - 号,就中括号的形式 element.style['text-align'] = '100px';
element.style.height = '100px';
2. 直接设置属性(只能用于某些属性,相关样式会自动识别)
element.setAttribute('height', 100);
element.setAttribute('height', '100px');
3. 设置 style 的属性
element.setAttribute('style', 'height: 100px !important');
4. 使用 setProperty 如果要设置!important,推荐用这种方法设置第三个参数
element.style.setProperty('height', '300px', 'important');
5. 改变 class 比如 JQ 的更改 class 相关方法
因 JS 获取不到 css 的伪元素,所以可以通过改变伪元素父级的 class 来动态更改伪元素的样式
element.className = 'blue';
element.className += 'blue fb';
6. 设置 cssText
element.style.cssText = 'height: 100px !important';
element.style.cssText += 'height: 100px !important';
7. 创建引入新的 css 样式文件
function addNewStyle(newStyle) {
var styleElement = document.getElementById('styles_js');
<span style="color: rgba(0, 0, 255, 1)">if (!<span style="color: rgba(0, 0, 0, 1)">styleElement) {
styleElement = document.createElement('style'<span style="color: rgba(0, 0, 0, 1)">);
styleElement.type = 'text/css'<span style="color: rgba(0, 0, 0, 1)">;
styleElement.id = 'styles_js'<span style="color: rgba(0, 0, 0, 1)">;
document.getElementsByTagName('head')[0<span style="color: rgba(0, 0, 0, 1)">].appendChild(styleElement);
}
styleElement.appendChild(document.createTextNode(newStyle));
}
addNewStyle('.box {height: 100px !important;}');</span></span></span></span></span></span></span></span></span></span></pre>
8. 使用 addRule、insertRule
// 在原有样式操作
document.styleSheets[0].addRule('.box', 'height: 100px');
document.styleSheets[0].insertRule('.box {height: 100px}', 0);
<span style="color: rgba(0, 128, 0, 1)">//<span style="color: rgba(0, 128, 0, 1)"> 或者插入新样式时操作
<span style="color: rgba(0, 0, 255, 1)">var styleEl = document.createElement('style'<span style="color: rgba(0, 0, 0, 1)">),
styleSheet =<span style="color: rgba(0, 0, 0, 1)"> styleEl.sheet;
styleSheet.addRule('.box', 'height: 100px'<span style="color: rgba(0, 0, 0, 1)">);
styleSheet.insertRule('.box {height: 100px}', 0<span style="color: rgba(0, 0, 0, 1)">);
document.head.appendChild(styleEl); </span></span></span></span></span></span></span></span></span></span></span></pre>