2016-07-05 38 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