如何利用 jQuery 修改 css 中带有 !important 的样式属性?

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

外部样式为:

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

通过 $("div.test").css("width","100px");$("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);
posted @ 2017-05-05 11:18  52php  阅读(1725)  评论(0编辑  收藏  举报