用 JQuery 覆盖身体背景?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9297281/
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-27 10:36:38  来源:igfitidea点击:

Overriding body background with JQuery?

jquerycss

提问by KRB

$('body').css('background-color', 'blue');does not work for me.

$('body').css('background-color', 'blue');对我不起作用。

There seems to be other matched CSS rules that prevail on the page.

页面上似乎还有其他匹配的 CSS 规则。

Is there a way I can force this CSS to work? This also doesn't work when I try background-image...

有没有办法强制这个 CSS 工作?当我尝试背景图像时,这也不起作用......

With Google Chrome's CSS explorer there are three different matched CSS rules for body.

使用 Google Chrome 的 CSS 资源管理器,可以为正文提供三种不同的匹配 CSS 规则。

I don't understand how this works and why this above JQuery isn't working.

我不明白这是如何工作的,以及为什么上面的 JQuery 不起作用。

Feed me knowledge please!

请给我知识!

[EDIT]

[编辑]

The answer is perfect for overriding CSS! Thanks

答案非常适合覆盖 CSS!谢谢

Note: Clear cache and browsing data from the beginning of time, it helps ;)

注意:从一开始就清除缓存和浏览数据,它会有所帮助;)

回答by ShankarSangoli

If you want to force this style to be applied in case there are conflicts then use !importantafter the style.

如果您想在发生冲突时强制应用此样式,请!important在样式之后使用。

$(function(){
    $('body').css('background-color', 'blue !important');
});

回答by Curt

Using !importantwill push the priority of this style:

使用!important将推动这种风格的优先级:

$('body').css('background-color', 'blue !important');

However it might be worth posting up more of your source code, as there is often a better solution than using !important

但是,可能值得发布更多的源代码,因为通常有比使用更好的解决方案 !important

回答by vdeantoni

Try to execute it after the page is loaded:

尝试在页面加载后执行:

$(window).load(function() {
  $('body').css('background', 'blue');
});

回答by iDifferent

As a quick solution, maybe try this:

作为一个快速的解决方案,也许试试这个:

$('body').css('background', 'blue !important');

But you should see where the conflicting is coming from and fix it.

但是您应该看到冲突的来源并修复它。

回答by Yinkci Heart

<script type="text/javascript">

    $(document).ready(function(){
          $("body").css("background-color","blue!important");
     });

</script>