2016-07-05 41 views
0

我有一个:如何使用jquery重写!important重要的内联样式?

<p style="color:red !important;">sample text here</p> 

,然后我想覆盖JQuery的这种风格,我会怎么做呢?

使用CSS !important显然是行不通的(但尝试也无妨)。 我在想我是否可以这样做:

$(document).ready(function(){ 
    $('p').css("color","#fff"); 
}); 

对此有何看法?

+0

它被重写你能指望什么? – guradio

+0

很适合我了jQuery没有工作 – Kobe1234

回答

0

你的代码是正确的,你可以做到这一点酷似你的建议。即使元素上已经有内联样式规则,一旦文档准备就绪,它将会用您写的内容替换该规则,即see here

DEMO

如果由于某种原因,你想要更多的保证,你可以应用与removeAttr() jQuery的方法,新造型前清除内嵌样式(但它是一个位的代码浪费)。

DEMO

$(document).ready(function(){ 
 
\t $('p').removeAttr('style'); 
 
    $('p').css("color","blue"); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<p style="color:red !important;">sample text here</p>

+0

是的,我对我的代码没有工作,直到我删除了样式属性。谢谢。 – Kobe1234