开发者

IE7 glitch with appendChild() for an input box

I am using the following JavaScript to add a number of inputs based on a number in a drop down menu. It works fine in firefox and chrome, but in IE7 the inputs all float left on a new line.

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", "");开发者_JAVA百科

//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;
}

}


I get the same bug in Firefox: you're adding the input as a child of listInput2, not of the li.

Here's a piece of code that works:

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;
}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜