javascript 如何在Javascript中设置元素的类属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20370700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:20:06 来源:igfitidea点击:
How to set class attribute of an element in Javascript?
提问by Neel
Here is my code for generate combobox. How can I add a css class name 'chosen-select' on it ?
这是我生成组合框的代码。如何在其上添加 css 类名“chosen-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);
回答by OlivierH
You can use className
property :
您可以使用className
属性:
element2.className = "chosen-select";