如何在Javascript中设置元素的class属性?

时间:2013-12-04 08:44:31

标签: javascript dom

这是我生成组合框的代码。如何在其上添加css类名“selected-select”?

var cell3 = row.insertCell(2);    
var element2 = document.createElement("select");   

var option1 = document.createElement("option");   
option1.value="";   
option1.innerHTML="--Select--";      
element2.appendChild(option1);   

var option2 = document.createElement("option");   
option2.value="1";   
option2.innerHTML="Apple";     
element2.appendChild(option2);   

element2.name = "description" + rowno;  
element2.id = "description" + rowno;   
element2.setAttribute("required","true");   
element2.style.width = "150px";   
cell3.appendChild(element2);   

1 个答案:

答案 0 :(得分:4)

您可以使用className属性:

element2.className = "chosen-select";

Documentation