javascript createElement()创建input不能设置name属性的解决方法
2009-10-11 00:00
931 查看
其中obj.name = "select";这句话并不能起到预期的作用,无论是看生成后的HTML代码还是观察使用document.getElementsByName()获取的数组长度,都会发现这个name设置并没有起作用。当然这种情况只出现在IE6和IE7中,而在FireFox中是没有问题的。
Test
function addInput(){
var div = document.getElementById("testDiv");
var obj = document.createElement("input");
obj.style.width = 63 + "px";
obj.style.height = 46 + "px";
obj.name = "select";
obj.setAttribute("id","button1");
document.getElementById("testDiv").appendChild(div);
}
function show(){
alert(document.getElementsByName("select").length);
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
当然解决方法很简单,在创建时,使用document.createElement("<input name='select'>")。不过还不知道是什么原因?
在Name属性的说明中有这样一句话:
In Internet Explorer 5, the name property cannot be set at run time on anchor objects dynamically created with the createElement method. To create an anchor with a NAME attribute, include the attribute and value when using the createElement method, or use the innerHTML property.
Test
function addInput(){
var div = document.getElementById("testDiv");
var obj = document.createElement("input");
obj.style.width = 63 + "px";
obj.style.height = 46 + "px";
obj.name = "select";
obj.setAttribute("id","button1");
document.getElementById("testDiv").appendChild(div);
}
function show(){
alert(document.getElementsByName("select").length);
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
当然解决方法很简单,在创建时,使用document.createElement("<input name='select'>")。不过还不知道是什么原因?
在Name属性的说明中有这样一句话:
In Internet Explorer 5, the name property cannot be set at run time on anchor objects dynamically created with the createElement method. To create an anchor with a NAME attribute, include the attribute and value when using the createElement method, or use the innerHTML property.
相关文章推荐
- javascript createElement()创建input不能设置name属性的解决方法
- JavaScript操作Word之“Automation 服务器不能创建对象”解决方法
- Qt 调用phonon 在xp下不能播放视频的解决方法(Phonon::createPath: Cannot connect Phonon::MediaObject ( no objectName )...)
- IE中document.createElement的iframe无法设置属性name问题
- IE中document.createElement的iframe无法设置属性name的解决方法
- 关于动态创建dsn时不能设置用户名和密码的解决方法
- 不能设置类range的columnwidth属性 解决方法
- JavaScript 覆盖document.createElement 方法 解决window.close在火狐下不兼容问题)
- js创建表格、创建input、判断浏览器类型、设置新创建的input的name属性
- Qt 调用phonon 在xp下不能播放视频的解决方法(Phonon::createPath: Cannot connect Phonon::MediaObject ( no objectName )...)
- JavaScript 覆盖document.createElement 方法 解决window.close在火狐下不兼容问题)
- IE、Firefox兼容form target当前页iframe,javascript动态创建表单对象form设置name属性
- JavaScript基础 创建节点 为节点添加属性 将节点挂载到body节点下 document.createElement setAttribute appendChild
- Qt 调用phonon 在xp下不能播放视频的解决方法(Phonon::createPath: Cannot connect Phonon::MediaObject ( no objectName )...)
- JavaScript中属性name与方法名不能相同
- JavaScript基础 创建节点 为节点添加属性 将节点挂载到body节点下 document.createElement setAttribute appendChild
- 在VS2008中创建自定义控件(十五)创建控件时出错(未能对属性XXX设置XXX)的解决方法
- CI框架 Cart类 name 不能设置中文的解决方法
- MVC dropdownlist 后端设置select属性后前端依然不能默认选中的解决方法
- Qt 调用phonon 在xp下不能播放视频的解决方法(Phonon::createPath: Cannot connect Phonon::MediaObject ( no objectName )...)