使用 jQuery 修改 css 中带有 !important 的样式属性

来源:互联网 发布:冯矿伟和讯网络课程 编辑:程序博客网 时间:2024/05/18 02:52

使用 jQuery 修改 css 中带有 !important 的样式属性

外部样式为:

   div.test {                width:auto !important;                overflow:auto !important            }

通过

 $("div.test").css("width","100px");  or $("div.test").css("width","100px !important"); 

是无效的


要想修改 div 的 width,可以通过如下这种方式:

$("div.test").css("cssText", "width:650px !important;");

要想修改多个属性,可以这么做:

$("div.test").css("cssText", "width:650px !important;overflow:hidden !important");

特别注意:

cssText 属性,会把先前的 css 值全部给覆盖掉,为了保留先前其他的样式,可以这么做:

var cssText = $("div.test").attr("style") + ";width:650px !important;";$("div.test").css("cssText": cssText);
原创粉丝点击