2017-07-16 65 views
0

添加多個屬性值僅適用於第一個,不知道爲什麼下面這種情況發生代碼:添加多個屬性值僅適用於第一個

jQuery("#gform_submit_button_1").attr({ 
    style: "color: #fefefe !important", 
    style: "background-color: #000000 !important" 

}); 

如果我改變自己的地方它仍然適用哪一個是第一。

回答

1

裝在一個屬性:

jQuery("#gform_submit_button_1").attr({ 
    style: "color: #fefefe !important; background-color: #000000 !important" 
}); 
1

陳述2個不同style標籤使秒一個重寫的第一個。如果您想更改多個屬性,必須通過他們在同一個style標籤,或者您可以使用jQuery .css而不是style

jQuery("#gform_submit_button_1").css({ 
    "color":, "#fefefe !important", 
    "background-color": "#000000 !important" 
}); 
1

這就是對象的方式工作。他們不允許有兩個值的一個關鍵:

console.log({ 
    style: "color: #fefefe !important", 
    style: "background-color: #000000 !important" 
}); 

所以,你可以加入他們在一起,這可以自動化:

var styles=["color: #fefefe !important","background-color: #000000 !important"]; 

jQuery("#gform_submit_button_1").attr({ 
style:styles.join(";")  
}); 
+0

由於生病記住這一點 – Zygimantas

相關問題