使用 css 或 jquery 覆盖 !important

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11962962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:09:15  来源:igfitidea点击:

Overriding !important with css or jquery

jquerycsscontent-script

提问by Wilson

(This is using content scripts in a chrome extension)

(这是在 chrome 扩展中使用内容脚本)

I need to overwrite some css properties that the webpage has labeled as !important. Is this possible?

我需要覆盖网页标记为的一些 css 属性!important。这可能吗?

For instance, if I want to get rid of the border that is labeled important:

例如,如果我想去掉标记为重要的边框:

$(".someclass").css('border','none'); //does not work

回答by ?ime Vidas

Here you go:

干得好:

$( '.someclass' ).each(function () {
    this.style.setProperty( 'border', 'none', 'important' );
});

Live demo:http://jsfiddle.net/Gtr54/

现场演示:http : //jsfiddle.net/Gtr54/

The .setPropertymethod of an element's styleobject enables you to pass a third argument which represents the priority. So, you're overriding an !important value with your own !important value. As far as I know, it is not possible to set the !important priority with jQuery, so your only option is the built-in .setPropertymethod.

.setProperty元素style对象的方法使您能够传递代表优先级的第三个参数。因此,您用自己的 !important 值覆盖了 !important 值。据我所知,无法使用 jQuery 设置 !important 优先级,因此您唯一的选择是内置.setProperty方法。

回答by hawkeye126

You can also do this:

你也可以这样做:

$(".someclass").css("cssText", "border: none !important;");

回答by Pallavi Dwivedi

This should help.

这应该有帮助。

$(".someclass").attr("style","border:none!important");

Updated, so as not to overwrite all styles:

更新,以免覆盖所有样式:

var existingStyles = $(".someclass").attr("style");
$(".someclass").attr("style", existingStyles+"border:none!important");

回答by Shubham Chopra

we can just add class using jquery

我们可以使用 jquery 添加类

$("someclass").addClass("test");

<style>
.test{
border:none !important;
}
</style>

回答by user2244656

there is also another way

还有另一种方式

$("#m_divList tbody").find("tr[data-uid=" + row.uid + "]").find('td').css("cssText", "color: red !important;");

css("cssText", "color: red !important;");

css("cssText", "颜色:红色!重要;");