3

在下面的 HTML 和 JavaScript 代码中,我希望在单击按钮时动态添加文本字段和单选按钮。这工作正常。但我也希望生成的新文本字段应该具有与其上方相同的字体大小和外观,即我想要具有相同的类。

这是HTML:

<input type="radio" name="choices"  value="o1"/>
<input type="text" name="o1" id="o1" class="inputtype"/>
<span class="file-wrapper">
<input type="file" name="o1_i" />
<span class="button">Choose an file</span>
</span>
<div id="responce"></div>

和 JavaScript:

var div = document.getElementById("responce");
var radio = document.createElement("input");
            radio.type = "radio";
            radio.name = "choices";
            radio.value = "o" + countBox;
            div.appendChild(radio);

var text = document.createElement("input");
            text.type = "text";
            text.id = "o" + countBox;
            text.name ="o" + countBox;
            div.appendChild(text);

var upload = document.createElement("input")
            upload.type = "file";
            upload.name= "o" + countBox + "_i";
            div.appendChild(upload);
4

2 回答 2

4

只需以className与设置名称、类型和 ID 相同的方式设置属性:

text.className = "inputtype";

对浏览按钮执行相同操作:

upload.className = "someclass";

对于多个类,用空格分隔它们。请注意,它会className覆盖整个班级列表。

于 2013-07-15T10:39:21.860 回答
0

如果您不想为javascript您添加的每个项目添加类。只需<input>使用 plain css:定位元素,#myform input[type=text]前提是您有一个form-tag或一些要定位的父元素。

于 2013-07-15T10:45:29.353 回答