【问题标题】:IE7 glitch with appendChild() for an input boxIE7 故障与 appendChild() 输入框
【发布时间】:2010-12-31 22:34:03
【问题描述】:

我正在使用以下 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;
}

}

【问题讨论】:

    标签: javascript dom forms input innerhtml


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      相关资源
      最近更新 更多