2016-05-15 58 views
0

形式輸入字段的設置風格,如果我有一個形式保存的所有字段的數組,JS - 從陣列

var reqfield = document.getElementsByClassName("req"); 

如何設置單個字段的CSS樣式基於所述陣列。

document.getElementById("myform")reqfield[1].style.backgroundColor = "red"; 
+0

只是刪除從'的document.getElementById的'的document.getElementById( 「myForm會」)'( 「myForm會」)reqfield [1]',爲什麼呢?你有很多這個ID的元素? – Sergio

回答

1

您已經將項目存儲在數組中。它應該只是

reqfield[1].style.backgroundColor = "red";