欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用jQuery修改CSS中带有!important的关键字的样式属性

最编程 2024-08-10 12:23:33
...

外部样式为:

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

通过 $("div.test").css("width","100px"); 和 $("div.test").css("width","100px !important"); 是无效的

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

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

 

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

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

 

特别注意:

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

1 1
2 2
3 var cssText = $("div.test").attr("style") + ";width:650px !important;";
4 $("div.test").css("cssText": cssText);