2011-01-06 29 views
2

如何應用相同的CSS屬性而不覆蓋和多次使用jQuery?如何應用相同的CSS屬性,而不覆蓋和多次使用jQuery?

$(function() { 

    $("#hello").css({ 

    // Is there a way to apply both of these instead of having the second override the first? 

    "background-image" : "-webkit-gradient(linear,left top,left bottom,color-stop(0, #444444),color-stop(1, #999999))", 
    "background-image" : "-moz-linear-gradient(top, #444444, #999999)" 

    }); 

});

回答

6

;分隔每個'指令'。

$("#hello").css({ 
    "background-image" : "-webkit-gradient(linear,left top,left bottom,color-stop(0, #444444),color-stop(1, #999999));-moz-linear-gradient(top, #444444, #999999)" 
}); 

或者,把它變成一個單一的CSS類,並做這麼多漂亮和管理的方式:

.gradient { 
    -webkit-gradient(linear,left top,left bottom,color-stop(0, #444444),color-stop(1, #999999)); 
    -moz-linear-gradient(top, #444444, #999999); 
} 


$("#hello").addClass("gradient"); 
+1

圍棋與CSS類。它從長遠來看肯定會有助於管理。 – chprpipr 2011-01-06 02:08:49

+0

我認爲在我上面的答案中最後一個顏色停止屬性之後出現了一個missin paren的問題。我修好了。 – karim79 2011-01-06 02:20:27

+0

對不起,但是JQuery技巧不起作用。 – Lastnico 2012-04-23 23:06:56