javascript createElement()创建input不能设置name属性的解决方法
作者:bea
其中obj.name = "select";这句话并不能起到预期的作用,无论是看生成后的HTML代码还是观察使用document.getElementsByName()获取的数组长度,都会发现这个name设置并没有起作用。当然这种情况只出现在IE6和IE7中,而在FireFox中是没有问题的。 <html> <head> <title>Test</title> </head> <body>
其中obj.name = "select";这句话并不能起到预期的作用,无论是看生成后的HTML代码还是观察使用document.getElementsByName()获取的数组长度,都会发现这个name设置并没有起作用。当然这种情况只出现在IE6和IE7中,而在FireFox中是没有问题的。
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="testDiv"></div>
<input type="button" onclick="addInput()" value="Test0">
<input type="button" onclick="show()" value="Test1">
<script type="text/javascript">
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);
}
</script>
</body>
</html>
[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.
有用 | 无用
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="testDiv"></div>
<input type="button" onclick="addInput()" value="Test0">
<input type="button" onclick="show()" value="Test1">
<script type="text/javascript">
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);
}
</script>
</body>
</html>
[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 变速加数功能实现代码
- JQuery 绑定事件时传递参数的实现方法
- 通用javascript脚本函数库 方便开发
- jQuery与javascript对照学习 获取父子前后元素 实现代码
- jquery pagination插件实现无刷新分页代码
- 让iframe子窗体取父窗体地址栏参数(querystring)
- javascript 判断某年某月有多少天的实现代码 推荐
- 百度留言本js 大家可以参考下
- javascript hashtable实现代码
- JavaScript 核心参考教程 RegExp对象
- JavaScript 核心参考教程 内置对象
- jQuery Ajax之load()方法
- jQuery Ajax之$.get()方法和$.post()方法
- JS 文件本身编码转换 图文教程
- jQuery 行级解析读取XML文件(附源码)
- css 有弹动效果的网页导航
- jquery select选中的一个小问题
- Jquery 表单取值赋值的一些基本操作
- jquery 框架使用教程 AJAX篇