使用具有!important标签的jquery更改div的背景颜色 [英] change background color of div with jquery that has an !important tag

查看:243
本文介绍了使用具有!important标签的jquery更改div的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一个调用.jsp的插件,该插件使用自己在外部托管的样式表(这是yelp嵌入的-试图操纵它的外观).我要更改的一个元素上面带有!important标记,我似乎无法对其进行更改...

Using a plugin that calls a .jsp that uses its own stylesheet hosted externally (it's a yelp embed - trying to manipulate the look of it). The one element I'm trying to change has an !important tag on it, and I can't seem to change it...

我尝试过

<script type="text/javascript">
   $('.elementToChange').css({'background-color':'black'});​
</script>

无济于事.想法?

推荐答案

在jQuery的最新版本(至少1.7.2和更高版本)中,您可以简单地设置css:

It looks like in more up-to-date versions of jQuery (at least 1.7.2 and higher) you can simply set the css:

$('#elementToChange').css('background-color', 'blue');

请参见 http://jsfiddle.net/HmXXc/185/

在旧版的jQu​​ery和Zepto中,您必须先清除css:

In older versions of jQuery and Zepto you had to clear the css first:

// Clear the !important css
$('#elementToChange').css('background-color', '');

然后使用以下命令将其重置:

And then reset it using:

$('#elementToChange').css('background-color', 'blue');

或采用单线形式:

$('#elementToChange')
    .css('background-color', '')
    .css('background-color', 'blue');

请参见 http://jsfiddle.net/HmXXc/186/.

原始答案:

注意:这可能不是一个好主意,因为它将删除任何其他内联样式

我将直接编辑样式属性

$('.elementToChange').attr('style', 'background-color: blue !important');

http://jsfiddle.net/RichardTowers/3wemT/1/

这篇关于使用具有!important标签的jquery更改div的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆