Internet Explorer 8 and later can set the NAME attribute at run time on elements dynamically created with the createElement method. To create an element with a NAME attribute in earlier versions of Internet Explorer, include the attribute and its value when using the createElement method.
也就是说, 我们必须通过带属性和值的标签来创建有 name 属性的元素. 为求各浏览器兼容良好, 代码可以这样写:
代码如下:
var element = null; try { // IE6/IE7 构建方式 element = document.createElement(''); } catch (e) { // W3C 构建方式 element = document.createElement('input'); element.name = 'radio-button'; } // 定义其他属性 element.id = 'radio-1' element.type = 'radio';