代码之家  ›  专栏  ›  技术社区  ›  Chris

为什么someinputeElement.type='button';在IE中失败?

  •  0
  • Chris  · 技术社区  · 16 年前

    我一直在尝试让以下javascript在IE中工作:

    var lastMonthBn = document.createElement('input');
    td.appendChild(lastMonthBn);
    lastMonthBn.value='<';
    lastMonthBn.type = 'button'; // Fails in IE
    lastMonthBn.setAttribute('type','button'); // Also fails in IE
    

    由于某种原因,我无法将输入设置为按钮,它失败了。适用于Chrome和Firefox。所以我有点困惑,没有任何运气让它工作。

    我使用alert()将它与这些行隔离。

    谢谢

    2 回复  |  直到 16 年前
        1
  •  3
  •   Vincent McNabb    16 年前

    对于IE,您需要先设置按钮,然后再将其添加到文档中。即。:

    var lastMonthBn = document.createElement('input');
    lastMonthBn.value='<';
    lastMonthBn.type = 'button';
    
    td.appendChild(lastMonthBn); // put this last
    
        2
  •  1
  •   Andy E    16 年前

    这就是原因吗?来自: http://msdn.microsoft.com/en-us/library/ms536389(v=VS.85).aspx

    使用createElement创建输入元素时,必须执行第二步。 createElement方法生成一个输入文本框,因为这是默认输入 类型属性。若要插入任何其他类型的输入元素,请首先调用 输入,然后在下一行代码中将类型属性设置为适当的值。