IE7故障与appendChild()为输入框

时间:2010-01-03 06:41:44

标签: javascript dom forms input innerhtml

我使用以下JavaScript根据下拉菜单中的数字添加一些输入。它在firefox和chrome中运行良好,但在IE7中,所有输入都在新行上浮动。

function add(number) 
{
var foo = document.getElementById("listInputs2");
var count = 1;
foo.innerHTML = "";

while ( count <= (number) )
{
//Create an input type dynamically.
var element = document.createElement("input");

//Assign different attributes to the element.
element.setAttribute("name", "value"+count);
element.setAttribute("id", "value"+count);
element.setAttribute("size", "60");
element.setAttribute("type", "text");
element.setAttribute("value", "");

//Append the element in page (in span).
foo.innerHTML+=('<li class="inputCount"><label for="value'+count+'">#'+count+'</label>');
foo.appendChild(element);
foo.innerHTML+=("</li>");
count += 1;
}

}

1 个答案:

答案 0 :(得分:0)

我在Firefox中遇到了同样的错误:您将输入添加为listInput2的子项,而不是li。

这是一段有用的代码:

function add(number) 
{
var foo = document.getElementById("listInputs2");
var count = 1;
foo.innerHTML = "";

while ( count <= (number) )
{
//Create an input type dynamically.
var input = "<input name='value"+count+"' id='value"+count+"' size='60' type='text' value=''>";

//Append the element in page (in span).
foo.innerHTML+='<li class="inputCount"><label for="value'+count+'">#'+count+'</label>'+input+'</li>';
count += 1;
}

}