JS设置CSS样式的几种方式(js设置!important)

原文地址:https://blog.csdn.net/x619y/article/details/80604609?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujs&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujs

//设置style的属性
element.setAttribute('style', 'height: 100px !important');

//使用setProperty  如果要设置!important,推荐用这种方法设置第三个参数,属性名不用驼峰写法
element.style.setProperty('height', '300px', 'important');

//设置cssText
element.style.cssText = 'height: 100px !important';
$("#test").css("cssText", "height:300px !important");
$("#test").css("style", "height:300px !important");//无效

  

  

原文地址:https://www.cnblogs.com/xi-li/p/14765701.html